From 55e4b2d2c32e96365fd0eb240394dc567118c3fd Mon Sep 17 00:00:00 2001 From: Marco Maccaferri Date: Mon, 4 Mar 2024 08:22:34 +0100 Subject: [PATCH] Info dialogs refactorings --- .../maccasoft/propeller/P1MemoryDialog.java | 123 +++++++++++----- .../maccasoft/propeller/P2MemoryDialog.java | 103 ++++++++++---- .../com/maccasoft/propeller/SpinTools.java | 134 +----------------- .../propeller/spin1/Spin1Object.java | 15 ++ .../propeller/spin2/Spin2Object.java | 13 ++ 5 files changed, 194 insertions(+), 194 deletions(-) diff --git a/modules/spin-tools/src/com/maccasoft/propeller/P1MemoryDialog.java b/modules/spin-tools/src/com/maccasoft/propeller/P1MemoryDialog.java index 70c70109..3255e5d6 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/P1MemoryDialog.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/P1MemoryDialog.java @@ -11,9 +11,12 @@ package com.maccasoft.propeller; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.text.NumberFormat; +import java.util.List; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; @@ -45,11 +48,13 @@ import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.ScrollBar; import org.eclipse.swt.widgets.Shell; +import com.maccasoft.propeller.internal.ColorRegistry; import com.maccasoft.propeller.spin1.Spin1Object; public class P1MemoryDialog extends Dialog { @@ -110,46 +115,46 @@ public void setTheme(String id) { listBackground = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND); if (Display.isSystemDarkTheme()) { - codeBackground = new Color(121, 0, 0); - variablesBackground = new Color(121, 121, 0); - stackFreeBackground = new Color(0, 121, 121); + codeBackground = ColorRegistry.getColor(121, 0, 0); + variablesBackground = ColorRegistry.getColor(121, 121, 0); + stackFreeBackground = ColorRegistry.getColor(0, 121, 121); } else { - codeBackground = new Color(255, 191, 191); - variablesBackground = new Color(255, 248, 192); - stackFreeBackground = new Color(191, 223, 255); + codeBackground = ColorRegistry.getColor(255, 191, 191); + variablesBackground = ColorRegistry.getColor(255, 248, 192); + stackFreeBackground = ColorRegistry.getColor(191, 223, 255); } } else if ("dark".equals(id)) { - widgetForeground = new Color(0xF0, 0xF0, 0xF0); - widgetBackground = new Color(0x50, 0x55, 0x57); - listForeground = new Color(0xA7, 0xA7, 0xA7); - listBackground = new Color(0x2B, 0x2B, 0x2B); - tabfolderBackground = new Color(0x43, 0x44, 0x47); - labelForeground = new Color(0xD7, 0xD7, 0xD7); - buttonBackground = new Color(0x50, 0x55, 0x57); - - codeBackground = new Color(121, 0, 0); - variablesBackground = new Color(121, 121, 0); - stackFreeBackground = new Color(0, 121, 121); + widgetForeground = ColorRegistry.getColor(0xF0, 0xF0, 0xF0); + widgetBackground = ColorRegistry.getColor(0x50, 0x55, 0x57); + listForeground = ColorRegistry.getColor(0xA7, 0xA7, 0xA7); + listBackground = ColorRegistry.getColor(0x2B, 0x2B, 0x2B); + tabfolderBackground = ColorRegistry.getColor(0x43, 0x44, 0x47); + labelForeground = ColorRegistry.getColor(0xD7, 0xD7, 0xD7); + buttonBackground = ColorRegistry.getColor(0x50, 0x55, 0x57); + + codeBackground = ColorRegistry.getColor(121, 0, 0); + variablesBackground = ColorRegistry.getColor(121, 121, 0); + stackFreeBackground = ColorRegistry.getColor(0, 121, 121); } else if ("light".equals(id)) { - widgetForeground = new Color(0x00, 0x00, 0x00); + widgetForeground = ColorRegistry.getColor(0x00, 0x00, 0x00); if ("win32".equals(Platform.PLATFORM)) { - widgetBackground = new Color(0xF0, 0xF0, 0xF0); + widgetBackground = ColorRegistry.getColor(0xF0, 0xF0, 0xF0); } else { - widgetBackground = new Color(0xFA, 0xFA, 0xFA); + widgetBackground = ColorRegistry.getColor(0xFA, 0xFA, 0xFA); } - listForeground = new Color(0x00, 0x00, 0x00); - listBackground = new Color(0xFE, 0xFE, 0xFE); + listForeground = ColorRegistry.getColor(0x00, 0x00, 0x00); + listBackground = ColorRegistry.getColor(0xFE, 0xFE, 0xFE); tabfolderBackground = widgetBackground; - labelForeground = new Color(0x00, 0x00, 0x00); - buttonBackground = new Color(0xFA, 0xFA, 0xFA); + labelForeground = ColorRegistry.getColor(0x00, 0x00, 0x00); + buttonBackground = ColorRegistry.getColor(0xFA, 0xFA, 0xFA); - codeBackground = new Color(255, 191, 191); - variablesBackground = new Color(255, 248, 192); - stackFreeBackground = new Color(191, 223, 255); + codeBackground = ColorRegistry.getColor(255, 191, 191); + variablesBackground = ColorRegistry.getColor(255, 248, 192); + stackFreeBackground = ColorRegistry.getColor(191, 223, 255); } } @@ -193,7 +198,7 @@ protected Control createDialogArea(Composite parent) { createInfoGroup(content); - tabFolder = new CTabFolder(content, SWT.BORDER); + tabFolder = new CTabFolder(content, SWT.BORDER | SWT.FLAT); tabFolder.setMaximizeVisible(false); tabFolder.setMinimizeVisible(false); tabFolder.setTabHeight(24); @@ -284,7 +289,7 @@ public void createInfoGroup(Composite parent) { label.setForeground(labelForeground); label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); label = new Label(group, SWT.NONE); - label.setText("$7FFF"); + label.setText(String.format("$%X", data.length - 1)); label.setForeground(labelForeground); label = new Label(group, SWT.BORDER); @@ -302,8 +307,8 @@ public void handleEvent(Event e) { e.gc.setBackground(stackFreeBackground); e.gc.fillRectangle(0, 0, bounds.width, bounds.height); - int codePixels = (int) (bounds.width * (vbase - pbase) / 32768.0); - int variablesPixels = (int) (bounds.width * (dbase - vbase) / 32768.0); + int codePixels = (int) (bounds.width * (vbase - pbase) / (double) data.length); + int variablesPixels = (int) (bounds.width * (dbase - vbase) / (double) data.length); int x = 0; e.gc.setBackground(codeBackground); @@ -761,10 +766,7 @@ public void setObject(Spin1Object object, ObjectTree tree, boolean topObject) { this.object = object; try { - byte[] binaryData = object.getBinary(); - - data = new byte[32 * 1024]; - System.arraycopy(binaryData, 0, data, 0, Math.min(data.length, binaryData.length)); + data = object.getEEprom(); clkfreq = object.getClkFreq(); clkmode = object.getClkMode(); @@ -803,11 +805,60 @@ protected void buttonPressed(int buttonId) { } protected void doSaveBinary() { - + File fileToSave = getFileToWrite(); + try { + FileOutputStream os = new FileOutputStream(fileToSave); + os.write(object.getBinary()); + os.close(); + } catch (Exception e) { + e.printStackTrace(); + } } protected void doSaveListing() { + File fileToSave = getFileToWrite(); + try { + PrintStream os = new PrintStream(new FileOutputStream(fileToSave)); + object.generateListing(os); + os.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + protected File getFileToWrite() { + FileDialog dlg = new FileDialog(getShell(), SWT.SAVE); + dlg.setOverwrite(true); + dlg.setText("Save Binary File"); + String[] filterNames = new String[] { + "Binary Files" + }; + String[] filterExtensions = new String[] { + "*.bin;*.binary" + }; + dlg.setFilterNames(filterNames); + dlg.setFilterExtensions(filterExtensions); + + String name = tree.getName(); + int i = name.lastIndexOf('.'); + dlg.setFileName(name.substring(0, i) + ".binary"); + + List lru = Preferences.getInstance().getLru(); + + File filterPath = tree.getFile(); + if (filterPath == null && lru.size() != 0) { + filterPath = new File(lru.get(0)); + } + if (filterPath != null) { + dlg.setFilterPath(filterPath.getParent()); + } + + String fileName = dlg.open(); + if (fileName != null) { + return new File(fileName); + } + return null; } } diff --git a/modules/spin-tools/src/com/maccasoft/propeller/P2MemoryDialog.java b/modules/spin-tools/src/com/maccasoft/propeller/P2MemoryDialog.java index 1d6d28d9..fdee0420 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/P2MemoryDialog.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/P2MemoryDialog.java @@ -11,9 +11,12 @@ package com.maccasoft.propeller; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.text.NumberFormat; +import java.util.List; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; @@ -45,6 +48,7 @@ import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.ScrollBar; @@ -113,9 +117,9 @@ public void setTheme(String id) { listBackground = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND); if (Display.isSystemDarkTheme()) { - codeBackground = new Color(121, 0, 0); - variablesBackground = new Color(121, 121, 0); - stackFreeBackground = new Color(0, 121, 121); + codeBackground = ColorRegistry.getColor(121, 0, 0); + variablesBackground = ColorRegistry.getColor(121, 121, 0); + stackFreeBackground = ColorRegistry.getColor(0, 121, 121); } else { codeBackground = ColorRegistry.getColor(255, 191, 191); @@ -124,31 +128,31 @@ public void setTheme(String id) { } } else if ("dark".equals(id)) { - widgetForeground = new Color(0xF0, 0xF0, 0xF0); - widgetBackground = new Color(0x50, 0x55, 0x57); - listForeground = new Color(0xA7, 0xA7, 0xA7); - listBackground = new Color(0x2B, 0x2B, 0x2B); - tabfolderBackground = new Color(0x43, 0x44, 0x47); - labelForeground = new Color(0xD7, 0xD7, 0xD7); - buttonBackground = new Color(0x50, 0x55, 0x57); - - codeBackground = new Color(121, 0, 0); - variablesBackground = new Color(121, 121, 0); - stackFreeBackground = new Color(0, 121, 121); + widgetForeground = ColorRegistry.getColor(0xF0, 0xF0, 0xF0); + widgetBackground = ColorRegistry.getColor(0x50, 0x55, 0x57); + listForeground = ColorRegistry.getColor(0xA7, 0xA7, 0xA7); + listBackground = ColorRegistry.getColor(0x2B, 0x2B, 0x2B); + tabfolderBackground = ColorRegistry.getColor(0x43, 0x44, 0x47); + labelForeground = ColorRegistry.getColor(0xD7, 0xD7, 0xD7); + buttonBackground = ColorRegistry.getColor(0x50, 0x55, 0x57); + + codeBackground = ColorRegistry.getColor(121, 0, 0); + variablesBackground = ColorRegistry.getColor(121, 121, 0); + stackFreeBackground = ColorRegistry.getColor(0, 121, 121); } else if ("light".equals(id)) { - widgetForeground = new Color(0x00, 0x00, 0x00); + widgetForeground = ColorRegistry.getColor(0x00, 0x00, 0x00); if ("win32".equals(Platform.PLATFORM)) { - widgetBackground = new Color(0xF0, 0xF0, 0xF0); + widgetBackground = ColorRegistry.getColor(0xF0, 0xF0, 0xF0); } else { - widgetBackground = new Color(0xFA, 0xFA, 0xFA); + widgetBackground = ColorRegistry.getColor(0xFA, 0xFA, 0xFA); } - listForeground = new Color(0x00, 0x00, 0x00); - listBackground = new Color(0xFE, 0xFE, 0xFE); + listForeground = ColorRegistry.getColor(0x00, 0x00, 0x00); + listBackground = ColorRegistry.getColor(0xFE, 0xFE, 0xFE); tabfolderBackground = widgetBackground; - labelForeground = new Color(0x00, 0x00, 0x00); - buttonBackground = new Color(0xFA, 0xFA, 0xFA); + labelForeground = ColorRegistry.getColor(0x00, 0x00, 0x00); + buttonBackground = ColorRegistry.getColor(0xFA, 0xFA, 0xFA); codeBackground = ColorRegistry.getColor(255, 191, 191); variablesBackground = ColorRegistry.getColor(255, 248, 192); @@ -771,10 +775,7 @@ public void setObject(Spin2Object object, ObjectTree tree, boolean topObject) { this.object = object; try { - byte[] binaryData = object.getBinary(); - - data = new byte[512 * 1024]; - System.arraycopy(binaryData, 0, data, 0, Math.min(data.length, binaryData.length)); + data = object.getFlash(); clkfreq = object.getClkFreq(); clkmode = object.getClkMode(); @@ -822,11 +823,61 @@ protected void buttonPressed(int buttonId) { } protected void doSaveBinary() { - + File fileToSave = getFileToWrite(); + try { + FileOutputStream os = new FileOutputStream(fileToSave); + object.setClockSetter(Preferences.getInstance().getSpin2ClockSetter()); + os.write(object.getBinary()); + os.close(); + } catch (Exception e) { + e.printStackTrace(); + } } protected void doSaveListing() { + File fileToSave = getFileToWrite(); + try { + PrintStream os = new PrintStream(new FileOutputStream(fileToSave)); + object.generateListing(os); + os.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + protected File getFileToWrite() { + FileDialog dlg = new FileDialog(getShell(), SWT.SAVE); + dlg.setOverwrite(true); + dlg.setText("Save Binary File"); + String[] filterNames = new String[] { + "Binary Files" + }; + String[] filterExtensions = new String[] { + "*.bin;*.binary" + }; + dlg.setFilterNames(filterNames); + dlg.setFilterExtensions(filterExtensions); + + String name = tree.getName(); + int i = name.lastIndexOf('.'); + dlg.setFileName(name.substring(0, i) + ".binary"); + + List lru = Preferences.getInstance().getLru(); + + File filterPath = tree.getFile(); + if (filterPath == null && lru.size() != 0) { + filterPath = new File(lru.get(0)); + } + if (filterPath != null) { + dlg.setFilterPath(filterPath.getParent()); + } + + String fileName = dlg.open(); + if (fileName != null) { + return new File(fileName); + } + return null; } } diff --git a/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java b/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java index 0cda4c1f..69dd9316 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java @@ -18,7 +18,6 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.file.Path; @@ -2514,148 +2513,19 @@ private void handleShowInfo() { } if (object instanceof Spin1Object) { - P1MemoryDialog dlg = new P1MemoryDialog(shell) { - - @Override - protected void doSaveBinary() { - try { - handleBinaryExport(getShell(), getObject()); - } catch (Exception e) { - e.printStackTrace(); - } - } - - @Override - protected void doSaveListing() { - try { - handleListingExport(getShell(), getObject()); - } catch (Exception e) { - e.printStackTrace(); - } - } - - }; + P1MemoryDialog dlg = new P1MemoryDialog(shell); dlg.setTheme(preferences.getTheme()); dlg.setObject((Spin1Object) object, editorTab.getObjectTree(), editorTab.isTopObject()); dlg.open(); } else if (object instanceof Spin2Object) { - P2MemoryDialog dlg = new P2MemoryDialog(shell) { - - @Override - protected void doSaveBinary() { - try { - handleBinaryExport(getShell(), getObject()); - } catch (Exception e) { - e.printStackTrace(); - } - } - - @Override - protected void doSaveListing() { - try { - handleListingExport(getShell(), getObject()); - } catch (Exception e) { - e.printStackTrace(); - } - } - - }; + P2MemoryDialog dlg = new P2MemoryDialog(shell); dlg.setTheme(preferences.getTheme()); dlg.setObject((Spin2Object) object, editorTab.getObjectTree(), editorTab.isTopObject()); dlg.open(); } } - private void handleBinaryExport(Shell shell, SpinObject object) { - EditorTab editorTab = getTargetObjectEditorTab(); - if (editorTab == null) { - return; - } - - FileDialog dlg = new FileDialog(shell, SWT.SAVE); - dlg.setOverwrite(true); - dlg.setText("Save Binary File"); - String[] filterNames = new String[] { - "Binary Files" - }; - String[] filterExtensions = new String[] { - "*.bin;*.binary" - }; - dlg.setFilterNames(filterNames); - dlg.setFilterExtensions(filterExtensions); - - String name = editorTab.getText(); - int i = name.lastIndexOf('.'); - dlg.setFileName(name.substring(0, i) + ".binary"); - - File filterPath = editorTab.getFile(); - if (filterPath == null && preferences.getLru().size() != 0) { - filterPath = new File(preferences.getLru().get(0)); - } - if (filterPath != null) { - dlg.setFilterPath(filterPath.getParent()); - } - - String fileName = dlg.open(); - if (fileName != null) { - File fileToSave = new File(fileName); - try { - FileOutputStream os = new FileOutputStream(fileToSave); - if (object instanceof Spin2Object) { - ((Spin2Object) object).setClockSetter(preferences.getSpin2ClockSetter()); - } - os.write(object.getBinary()); - os.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - private void handleListingExport(Shell shell, SpinObject object) { - EditorTab editorTab = getTargetObjectEditorTab(); - if (editorTab == null) { - return; - } - - FileDialog dlg = new FileDialog(shell, SWT.SAVE); - dlg.setOverwrite(true); - dlg.setText("Save Listing File"); - String[] filterNames = new String[] { - "Listing Files" - }; - String[] filterExtensions = new String[] { - "*.lst;*.txt" - }; - dlg.setFilterNames(filterNames); - dlg.setFilterExtensions(filterExtensions); - - String name = editorTab.getText(); - int i = name.lastIndexOf('.'); - dlg.setFileName(name.substring(0, i) + ".lst"); - - File filterPath = editorTab.getFile(); - if (filterPath == null && preferences.getLru().size() != 0) { - filterPath = new File(preferences.getLru().get(0)); - } - if (filterPath != null) { - dlg.setFilterPath(filterPath.getParent()); - } - - String fileName = dlg.open(); - if (fileName != null) { - File fileToSave = new File(fileName); - try { - PrintStream ps = new PrintStream(new FileOutputStream(fileToSave)); - object.generateListing(ps); - ps.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - private void handleUpload(boolean writeToFlash, boolean openTerminal, boolean forceDebug) { SerialPort serialPort = null; boolean serialPortShared = false; diff --git a/modules/spin-tools/src/com/maccasoft/propeller/spin1/Spin1Object.java b/modules/spin-tools/src/com/maccasoft/propeller/spin1/Spin1Object.java index ce33c44b..d06a9f8d 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/spin1/Spin1Object.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/spin1/Spin1Object.java @@ -10,6 +10,9 @@ package com.maccasoft.propeller.spin1; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + import com.maccasoft.propeller.SpinObject; public class Spin1Object extends SpinObject { @@ -58,4 +61,16 @@ public void setDcurr(int dcurr) { this.dcurr = dcurr; } + public byte[] getEEprom() throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + generateBinary(os); + + int count = (32 * 1024) - os.size(); + if (count > 0) { + os.write(new byte[count]); + } + + return os.toByteArray(); + } + } diff --git a/modules/spin-tools/src/com/maccasoft/propeller/spin2/Spin2Object.java b/modules/spin-tools/src/com/maccasoft/propeller/spin2/Spin2Object.java index d68ee1ae..10abce90 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/spin2/Spin2Object.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/spin2/Spin2Object.java @@ -10,6 +10,7 @@ package com.maccasoft.propeller.spin2; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -211,4 +212,16 @@ void writeLong(byte[] code, int index, int value) { code[index + 3] = (byte) (value >> 24); } + public byte[] getFlash() throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + generateBinary(os); + + int count = (512 * 1024) - os.size(); + if (count > 0) { + os.write(new byte[count]); + } + + return os.toByteArray(); + } + }