added a file history to the fsm dialog

This commit is contained in:
hneemann 2018-12-07 09:03:03 +01:00
parent f7808f0969
commit 6ff1b2d651
5 changed files with 63 additions and 46 deletions

View File

@ -40,28 +40,28 @@
<transitions>
<transition>
<values></values>
<position x="777.1327" y="182.86734"/>
<position x="742.5" y="217.50002"/>
<fromState reference="../../../states/state"/>
<toState reference="../../../states/state[2]"/>
<condition></condition>
</transition>
<transition>
<values></values>
<position x="777.13477" y="537.13477"/>
<position x="743.5" y="503.5"/>
<fromState reference="../../../states/state[2]"/>
<toState reference="../../../states/state[3]"/>
<condition></condition>
</transition>
<transition>
<values></values>
<position x="428.74414" y="531.25586"/>
<position x="465.0" y="495.0"/>
<fromState reference="../../../states/state[3]"/>
<toState reference="../../../states/state[4]"/>
<condition></condition>
</transition>
<transition>
<values></values>
<position x="428.7441" y="188.74411"/>
<position x="463.0" y="223.0"/>
<fromState reference="../../../states/state[4]"/>
<toState reference="../../../states/state"/>
<condition></condition>

View File

@ -2,76 +2,76 @@
<fsm>
<states>
<state>
<values>R=1</values>
<position x="600.0" y="180.0"/>
<number>1</number>
<name>Red</name>
<radius>70</radius>
<values>R=1</values>
</state>
<state>
<values>R=1,Y=1</values>
<position x="780.0" y="360.0"/>
<number>3</number>
<name>Red/Yellow</name>
<radius>70</radius>
<values>R=1,Y=1</values>
</state>
<state>
<values>G=1</values>
<position x="600.0" y="540.0"/>
<number>4</number>
<name>Green</name>
<radius>70</radius>
<values>G=1</values>
</state>
<state>
<values>Y=1</values>
<position x="420.0" y="360.0"/>
<number>2</number>
<name>Yellow</name>
<radius>70</radius>
<values>Y=1</values>
</state>
<state>
<values></values>
<position x="420.0" y="120.0"/>
<number>0</number>
<name>init</name>
<radius>70</radius>
<values></values>
</state>
</states>
<transitions>
<transition>
<values></values>
<position x="743.5" y="216.5"/>
<fromState reference="../../../states/state"/>
<toState reference="../../../states/state[2]"/>
<condition></condition>
<values></values>
</transition>
<transition>
<values></values>
<position x="742.5" y="502.5"/>
<fromState reference="../../../states/state[2]"/>
<toState reference="../../../states/state[3]"/>
<condition></condition>
<values></values>
</transition>
<transition>
<values></values>
<position x="459.0" y="501.0"/>
<fromState reference="../../../states/state[3]"/>
<toState reference="../../../states/state[4]"/>
<condition></condition>
<values></values>
</transition>
<transition>
<values></values>
<position x="463.5" y="223.5"/>
<fromState reference="../../../states/state[4]"/>
<toState reference="../../../states/state"/>
<condition></condition>
<values></values>
</transition>
<transition>
<position x="510.5" y="148.5"/>
<values></values>
<position x="487.4829" y="138.4943"/>
<fromState reference="../../../states/state[5]"/>
<toState reference="../../../states/state"/>
<condition></condition>
<values></values>
</transition>
</transitions>
</fsm>

View File

@ -12,10 +12,7 @@ import de.neemann.digital.draw.library.ElementLibrary;
import de.neemann.digital.draw.shapes.ShapeFactory;
import de.neemann.digital.fsm.FSM;
import de.neemann.digital.fsm.FSMDemos;
import de.neemann.digital.gui.Main;
import de.neemann.digital.gui.ModelCreationListener;
import de.neemann.digital.gui.SaveAsHelper;
import de.neemann.digital.gui.Settings;
import de.neemann.digital.gui.*;
import de.neemann.digital.gui.components.table.TableDialog;
import de.neemann.digital.lang.Lang;
import de.neemann.gui.*;
@ -36,7 +33,7 @@ import java.util.prefs.Preferences;
/**
* The dialog to show the FSM
*/
public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSave, FSM.ModifiedListener, ModelCreationListener {
public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSave, FSM.ModifiedListener, ModelCreationListener, FileHistory.OpenInterface {
private static final Preferences PREFS = Preferences.userRoot().node("dig").node("fsm");
private static final String PREF_FOLDER = "folder";
private static final Icon ICON_NEW = IconCreator.create("document-new.png");
@ -47,10 +44,11 @@ public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSav
private static final Icon ICON_ZOOM_IN = IconCreator.create("View-zoom-in.png");
private static final Icon ICON_ZOOM_OUT = IconCreator.create("View-zoom-out.png");
private FSM fsm;
private final FileHistory fileHistory;
private final FSMComponent fsmComponent;
private final Timer timer;
private final JComboBox<String> moveControl;
private FSM fsm;
private boolean moveStates = false;
private ToolTipAction save;
private File filename;
@ -60,17 +58,14 @@ public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSav
/**
* Creates a new instance
*
* @param parent the parents frame
* @param givenFsm the fsm to visualize
* @param library the library used to show the table
* @param parent the parents frame
* @param library the library used to show the table
*/
public FSMFrame(JFrame parent, FSM givenFsm, ElementLibrary library) {
public FSMFrame(JFrame parent, ElementLibrary library) {
super(Lang.get("fsm_title"));
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
if (givenFsm == null) {
givenFsm = FSMDemos.rotDecoder();
givenFsm.circle();
}
fileHistory = new FileHistory(this, PREFS.node("hist"));
fsmComponent = new FSMComponent();
getContentPane().add(fsmComponent, BorderLayout.CENTER);
@ -135,7 +130,11 @@ public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSav
setJMenuBar(bar);
pack();
setFSM(givenFsm);
setFSM(new FSM());
File f = fileHistory.getMostRecent();
if (f != null)
SwingUtilities.invokeLater(() -> loadFile(f));
setLocationRelativeTo(parent);
}
@ -157,15 +156,14 @@ public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSav
if (ClosingWindowListener.checkForSave(FSMFrame.this, FSMFrame.this)) {
JFileChooser fc = getJFileChooser(filename);
if (fc.showOpenDialog(FSMFrame.this) == JFileChooser.APPROVE_OPTION) {
loadFile(fc.getSelectedFile());
loadFile(SaveAsHelper.checkSuffix(fc.getSelectedFile(), "fsm"));
}
}
}
}.setAcceleratorCTRLplus('O');
// JMenu openRecent = new JMenu(Lang.get("menu_openRecent"));
// JMenu openRecentNewWindow = new JMenu(Lang.get("menu_openRecentNewWindow"));
// fileHistory.setMenu(openRecent, openRecentNewWindow);
JMenu openRecent = new JMenu(Lang.get("menu_openRecent"));
fileHistory.setMenu(openRecent, null);
ToolTipAction saveAs = new ToolTipAction(Lang.get("menu_saveAs"), ICON_SAVE_AS) {
@Override
@ -195,6 +193,7 @@ public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSav
bar.add(file);
file.add(newFile.createJMenuItem());
file.add(open.createJMenuItem());
file.add(openRecent);
file.add(save.createJMenuItem());
file.add(saveAs.createJMenuItem());
file.add(export);
@ -241,6 +240,8 @@ public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSav
setTitle(fsmTitle);
this.filename = filename;
if (filename != null)
fileHistory.add(filename);
}
@Override
@ -292,6 +293,12 @@ public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSav
}
}
@Override
public void open(File file, boolean newWindow) {
if (ClosingWindowListener.checkForSave(FSMFrame.this, FSMFrame.this))
loadFile(file);
}
private void createViewMenu(JMenuBar menuBar, JToolBar toolBar) {
ToolTipAction maximize = new ToolTipAction(Lang.get("menu_maximize"), ICON_EXPAND) {
@Override
@ -468,12 +475,10 @@ public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSav
* @param args the programs arguments
*/
public static void main(String[] args) {
FSM fsm = FSMDemos.rotDecoder();
ElementLibrary library = new ElementLibrary();
new ShapeFactory(library);
new FSMFrame(null, fsm.circle().setModified(false), library).setVisible(true);
new FSMFrame(null, library).setVisible(true);
}
}

View File

@ -17,13 +17,13 @@ import java.util.prefs.Preferences;
* History of last opened files
*/
public final class FileHistory {
private static final Preferences PREFS = Preferences.userRoot().node("dig").node("hist");
private static final String FILE_NUM = "fileNum";
private static final String FILE_NAME = "name";
private static final int MAX_SIZE = 15;
private final ArrayList<File> files;
private final OpenInterface opener;
private final Preferences prefs;
private JMenu menu;
private JMenu menuNewWindow;
@ -33,11 +33,22 @@ public final class FileHistory {
* @param opener the opene interface to be used to open a file
*/
public FileHistory(OpenInterface opener) {
this(opener, Preferences.userRoot().node("dig").node("hist"));
}
/**
* Creates a new instance
*
* @param opener the opene interface to be used to open a file
* @param prefs the preferences node to store the history
*/
public FileHistory(OpenInterface opener, Preferences prefs) {
this.opener = opener;
int n = PREFS.getInt(FILE_NUM, 0);
this.prefs = prefs;
int n = prefs.getInt(FILE_NUM, 0);
files = new ArrayList<File>();
for (int i = 0; i < n; i++) {
String pathname = PREFS.get(FILE_NAME + i, null);
String pathname = prefs.get(FILE_NAME + i, null);
if (pathname != null && pathname.length() > 0) {
final File file = new File(pathname);
if (file.exists())
@ -49,9 +60,9 @@ public final class FileHistory {
}
private void saveEntries() {
PREFS.putInt(FILE_NUM, files.size());
prefs.putInt(FILE_NUM, files.size());
for (int i = 0; i < files.size(); i++)
PREFS.put(FILE_NAME + i, files.get(i).getPath());
prefs.put(FILE_NAME + i, files.get(i).getPath());
}
/**
@ -89,10 +100,12 @@ public final class FileHistory {
private void updateMenu() {
if (menu != null) {
menu.removeAll();
menuNewWindow.removeAll();
if (menuNewWindow != null)
menuNewWindow.removeAll();
for (File f : files) {
menu.add(new FileOpenEntry(f, opener, false).createJMenuItem());
menuNewWindow.add(new FileOpenEntry(f, opener, true).createJMenuItem());
if (menuNewWindow != null)
menuNewWindow.add(new FileOpenEntry(f, opener, true).createJMenuItem());
}
}
}

View File

@ -28,7 +28,6 @@ import de.neemann.digital.draw.model.ModelCreator;
import de.neemann.digital.draw.model.RealTimeClock;
import de.neemann.digital.draw.shapes.Drawable;
import de.neemann.digital.draw.shapes.ShapeFactory;
import de.neemann.digital.fsm.FSM;
import de.neemann.digital.fsm.gui.FSMFrame;
import de.neemann.digital.gui.components.*;
import de.neemann.digital.gui.components.data.GraphDialog;
@ -1138,7 +1137,7 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
analyse.add(new ToolTipAction(Lang.get("menu_fsm")) {
@Override
public void actionPerformed(ActionEvent e) {
new FSMFrame(Main.this, new FSM(), library)
new FSMFrame(Main.this, library)
.setBaseFileName(filename)
.registerTo(Main.this)
.setVisible(true);