open nested models by clicking right

This commit is contained in:
hneemann 2016-03-27 22:54:50 +02:00
parent 59bc557fc1
commit bd5f37fdf0
4 changed files with 47 additions and 25 deletions

View File

@ -33,7 +33,7 @@ public final class ShapeFactory {
return InstanceHolder.INSTANCE; return InstanceHolder.INSTANCE;
} }
public HashMap<String, Creator> map = new HashMap<>(); private HashMap<String, Creator> map = new HashMap<>();
private ElementLibrary library; private ElementLibrary library;
private ShapeFactory() { private ShapeFactory() {

View File

@ -1,5 +1,6 @@
package de.neemann.digital.gui; package de.neemann.digital.gui;
import de.neemann.digital.core.element.ElementFactory;
import de.neemann.digital.core.element.ElementTypeDescription; import de.neemann.digital.core.element.ElementTypeDescription;
import de.neemann.digital.draw.elements.Circuit; import de.neemann.digital.draw.elements.Circuit;
import de.neemann.digital.draw.elements.VisualElement; import de.neemann.digital.draw.elements.VisualElement;
@ -13,6 +14,7 @@ import de.neemann.gui.ErrorMessage;
import de.neemann.gui.ToolTipAction; import de.neemann.gui.ToolTipAction;
import javax.swing.*; import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -22,7 +24,6 @@ import java.util.ArrayList;
*/ */
public class LibrarySelector implements ElementNotFoundNotification { public class LibrarySelector implements ElementNotFoundNotification {
private final ElementLibrary library; private final ElementLibrary library;
private File lastFile;
private File filePath; private File filePath;
private JMenu customMenu; private JMenu customMenu;
private InsertHistory insertHistory; private InsertHistory insertHistory;
@ -46,7 +47,8 @@ public class LibrarySelector implements ElementNotFoundNotification {
customMenu.add(new ToolTipAction(Lang.get("menu_import")) { customMenu.add(new ToolTipAction(Lang.get("menu_import")) {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JFileChooser fc = Main.getjFileChooser(lastFile); JFileChooser fc = new JFileChooser(filePath);
fc.addChoosableFileFilter(new FileNameExtensionFilter("Circuit", "dig"));
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
importElement(fc.getSelectedFile()); importElement(fc.getSelectedFile());
} }
@ -79,10 +81,6 @@ public class LibrarySelector implements ElementNotFoundNotification {
return parts; return parts;
} }
public void setLastFile(File lastFile) {
this.lastFile = lastFile;
}
public void setFilePath(File filePath) { public void setFilePath(File filePath) {
this.filePath = filePath; this.filePath = filePath;
} }
@ -118,7 +116,7 @@ public class LibrarySelector implements ElementNotFoundNotification {
System.out.println("load element " + file); System.out.println("load element " + file);
Circuit circuit = Circuit.loadCircuit(file); Circuit circuit = Circuit.loadCircuit(file);
ElementTypeDescription description = ElementTypeDescription description =
new ElementTypeDescription(file.getName(), new ElementTypeDescriptionCustom(file,
attributes -> new CustomElement(circuit, library, file.getName()), attributes -> new CustomElement(circuit, library, file.getName()),
circuit.getInputNames(library)) circuit.getInputNames(library))
.setShortName(createShortName(file)); .setShortName(createShortName(file));
@ -150,4 +148,17 @@ public class LibrarySelector implements ElementNotFoundNotification {
this.menuEntry = menuEntry; this.menuEntry = menuEntry;
} }
} }
public static class ElementTypeDescriptionCustom extends ElementTypeDescription {
private final File file;
public ElementTypeDescriptionCustom(File file, ElementFactory elementFactory, String... inputNames) {
super(file.getName(), elementFactory, inputNames);
this.file = file;
}
public File getFile() {
return file;
}
}
} }

View File

@ -53,7 +53,11 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
private Model model; private Model model;
private ModelDescription modelDescription; private ModelDescription modelDescription;
public Main(File fileToOpen) { public Main() {
this(null, null);
}
public Main(Component parent, File fileToOpen) {
super(Lang.get("digital")); super(Lang.get("digital"));
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
@ -73,10 +77,6 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
addWindowListener(new ClosingWindowListener(this, this)); addWindowListener(new ClosingWindowListener(this, this));
setPreferredSize(new Dimension(800, 600));
pack();
setLocationRelativeTo(null);
JMenuBar bar = new JMenuBar(); JMenuBar bar = new JMenuBar();
@ -108,7 +108,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
if (ClosingWindowListener.checkForSave(Main.this, Main.this)) { if (ClosingWindowListener.checkForSave(Main.this, Main.this)) {
JFileChooser fc = getjFileChooser(lastFilename); JFileChooser fc = getjFileChooser(lastFilename);
if (fc.showOpenDialog(Main.this) == JFileChooser.APPROVE_OPTION) { if (fc.showOpenDialog(Main.this) == JFileChooser.APPROVE_OPTION) {
Main m = new Main(fc.getSelectedFile()); Main m = new Main(Main.this, fc.getSelectedFile());
m.setLocationRelativeTo(Main.this); m.setLocationRelativeTo(Main.this);
m.setVisible(true); m.setVisible(true);
} }
@ -261,10 +261,14 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
setJMenuBar(bar); setJMenuBar(bar);
InfoDialog.getInstance().addToFrame(this, MESSAGE); InfoDialog.getInstance().addToFrame(this, MESSAGE);
setPreferredSize(new Dimension(800, 600));
pack();
setLocationRelativeTo(parent);
} }
public static void main(String[] args) { public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Main(null).setVisible(true)); SwingUtilities.invokeLater(() -> new Main().setVisible(true));
} }
private void createAndStartModel(boolean runClock) { private void createAndStartModel(boolean runClock) {
@ -309,7 +313,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
} }
} }
public static JFileChooser getjFileChooser(File filename) { private static JFileChooser getjFileChooser(File filename) {
JFileChooser fileChooser = new JFileChooser(filename == null ? null : filename.getParentFile()); JFileChooser fileChooser = new JFileChooser(filename == null ? null : filename.getParentFile());
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Circuit", "dig")); fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Circuit", "dig"));
return fileChooser; return fileChooser;
@ -352,7 +356,6 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
private void setFilename(File filename) { private void setFilename(File filename) {
this.filename = filename; this.filename = filename;
if (filename != null) { if (filename != null) {
librarySelector.setLastFile(filename);
this.lastFilename = filename; this.lastFilename = filename;
prefs.put("name", filename.getPath()); prefs.put("name", filename.getPath());
setTitle(filename + " - " + Lang.get("digital")); setTitle(filename + " - " + Lang.get("digital"));

View File

@ -2,6 +2,7 @@ package de.neemann.digital.gui.components;
import de.neemann.digital.core.Observer; import de.neemann.digital.core.Observer;
import de.neemann.digital.core.element.AttributeKey; import de.neemann.digital.core.element.AttributeKey;
import de.neemann.digital.core.element.ElementTypeDescription;
import de.neemann.digital.draw.elements.Circuit; import de.neemann.digital.draw.elements.Circuit;
import de.neemann.digital.draw.elements.Moveable; import de.neemann.digital.draw.elements.Moveable;
import de.neemann.digital.draw.elements.VisualElement; import de.neemann.digital.draw.elements.VisualElement;
@ -11,6 +12,8 @@ import de.neemann.digital.draw.graphics.Polygon;
import de.neemann.digital.draw.library.ElementLibrary; import de.neemann.digital.draw.library.ElementLibrary;
import de.neemann.digital.draw.shapes.Drawable; import de.neemann.digital.draw.shapes.Drawable;
import de.neemann.digital.draw.shapes.GenericShape; import de.neemann.digital.draw.shapes.GenericShape;
import de.neemann.digital.gui.LibrarySelector;
import de.neemann.digital.gui.Main;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -316,7 +319,11 @@ public class CircuitComponent extends JComponent implements Observer {
VisualElement vp = circuit.getElementAt(getPosVector(e)); VisualElement vp = circuit.getElementAt(getPosVector(e));
if (vp != null) { if (vp != null) {
String name = vp.getElementName(); String name = vp.getElementName();
ArrayList<AttributeKey> list = library.getElementType(name).getAttributeList(); ElementTypeDescription elementType = library.getElementType(name);
if (elementType instanceof LibrarySelector.ElementTypeDescriptionCustom) {
new Main(this, ((LibrarySelector.ElementTypeDescriptionCustom) elementType).getFile()).setVisible(true);
} else {
ArrayList<AttributeKey> list = elementType.getAttributeList();
if (list.size() > 0) { if (list.size() > 0) {
Point p = new Point(e.getX(), e.getY()); Point p = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(p, CircuitComponent.this); SwingUtilities.convertPointToScreen(p, CircuitComponent.this);
@ -327,6 +334,7 @@ public class CircuitComponent extends JComponent implements Observer {
return true; return true;
} }
} }
}
return false; return false;
} }