1 package com.billkuker.rocketry.motorsim.visual;
\r
2 import java.awt.Component;
\r
3 import java.awt.FileDialog;
\r
4 import java.awt.Frame;
\r
5 import java.awt.event.ActionEvent;
\r
6 import java.awt.event.ActionListener;
\r
8 import java.io.IOException;
\r
9 import java.util.HashMap;
\r
10 import java.util.HashSet;
\r
11 import java.util.List;
\r
12 import java.util.Map;
\r
13 import java.util.Set;
\r
14 import java.util.Vector;
\r
16 import javax.swing.JMenu;
\r
17 import javax.swing.JMenuItem;
\r
18 import javax.swing.JOptionPane;
\r
19 import javax.swing.JTabbedPane;
\r
21 import org.apache.log4j.Logger;
\r
24 public abstract class MultiObjectEditor<OBJECT, EDITOR extends Component> extends JTabbedPane {
\r
26 private static final long serialVersionUID = 1L;
\r
28 private static final Logger log = Logger.getLogger(MultiObjectEditor.class);
\r
30 protected abstract class ObjectCreator {
\r
31 public abstract OBJECT newObject();
\r
32 public abstract String getName();
\r
35 private final Frame frame;
\r
37 private final String noun;
\r
39 private Set<ObjectCreator> creators = new HashSet<ObjectCreator>();
\r
41 private final Map<OBJECT, EDITOR> objectToEditor = new HashMap<OBJECT, EDITOR>();
\r
42 private final Map<EDITOR, OBJECT> editorToObject = new HashMap<EDITOR, OBJECT>();
\r
43 private final Map<File, EDITOR> fileToEditor = new HashMap<File, EDITOR>();
\r
44 private final Map<EDITOR, File> editorToFile = new HashMap<EDITOR, File>();
\r
46 private final Set<OBJECT> dirty = new HashSet<OBJECT>();
\r
48 public MultiObjectEditor(final Frame frame, final String noun){
\r
50 this.noun = " " + noun.trim();
\r
53 protected final void addCreator(ObjectCreator c){
\r
57 public final void dirty(final OBJECT o){
\r
58 if ( !dirty.contains(o) )
\r
59 setTitleAt(indexOfComponent(objectToEditor.get(o)), "*" + getTitleAt(indexOfComponent(objectToEditor.get(o))));
\r
63 public final void dirty(final EDITOR e){
\r
64 dirty(editorToObject.get(e));
\r
67 private final void undirty(final OBJECT o){
\r
68 if ( dirty.contains(o) )
\r
69 setTitleAt(indexOfComponent(objectToEditor.get(o)), getTitleAt(indexOfComponent(objectToEditor.get(o))).replaceAll("^\\*", ""));
\r
73 public final JMenu getMenu(){
\r
74 JMenu ret = new JMenu("File");
\r
75 for ( JMenuItem i : getMenuItems() )
\r
80 private void menuNew(ObjectCreator c){
\r
84 @SuppressWarnings("unchecked")
\r
86 public EDITOR getSelectedComponent(){
\r
87 return (EDITOR)super.getSelectedComponent();
\r
90 private void close(){
\r
91 EDITOR e = getSelectedComponent();
\r
92 OBJECT o = editorToObject.get(e);
\r
93 File f = editorToFile.get(e);
\r
95 if ( dirty.contains(o) ){
\r
96 int response = JOptionPane.showConfirmDialog(this, "Object is unsaved. Save Before Closing?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
\r
97 if ( response == JOptionPane.YES_OPTION ){
\r
99 } else if ( response == JOptionPane.CANCEL_OPTION ){
\r
104 objectToEditor.remove(o);
\r
105 editorToObject.remove(e);
\r
106 fileToEditor.remove(f);
\r
107 editorToFile.remove(e);
\r
111 private void saveDialog(){
\r
112 EDITOR e = getSelectedComponent();
\r
113 if ( !editorToFile.containsKey(e) ){
\r
114 log.debug("Editor has no file, saving as...");
\r
118 File file = editorToFile.get(e);
\r
119 log.debug("Saving to " + file.getAbsolutePath());
\r
121 saveToFile(editorToObject.get(e), file);
\r
122 undirty(editorToObject.get(e));
\r
123 } catch (IOException e1) {
\r
127 private void saveAsDialog(){
\r
128 EDITOR e = getSelectedComponent();
\r
129 final FileDialog fd = new FileDialog(frame, "Save" + noun + " As", FileDialog.SAVE);
\r
130 fd.setVisible(true);
\r
131 if (fd.getFile() != null ) {
\r
132 File file = new File(fd.getDirectory() + fd.getFile());
\r
134 OBJECT o = editorToObject.get(e);
\r
135 saveToFile(o, file);
\r
137 objectToEditor.put(o, e);
\r
138 editorToObject.put(e, o);
\r
139 fileToEditor.put(file, e);
\r
140 editorToFile.put(e, file);
\r
142 getSelectedIndex(),
\r
144 } catch (Exception e1) {
\r
150 private void openDialog(){
\r
151 final FileDialog fd = new FileDialog(frame, "Open" + noun + "...", FileDialog.LOAD);
\r
152 fd.setVisible(true);
\r
153 if ( fd.getFile() != null ) {
\r
154 File file = new File(fd.getDirectory() + fd.getFile());
\r
155 log.debug("Opening File " + file.getAbsolutePath());
\r
156 if ( fileToEditor.containsKey(file) ){
\r
157 log.debug("File " + file.getAbsolutePath() + "Already open, focusing");
\r
158 setSelectedComponent(fileToEditor.get(file));
\r
162 OBJECT o = loadFromFile(file);
\r
163 EDITOR e = createEditor(o);
\r
164 objectToEditor.put(o, e);
\r
165 editorToObject.put(e, o);
\r
166 fileToEditor.put(file, e);
\r
167 editorToFile.put(e, file);
\r
168 addTab(file.getName(), e);
\r
169 } catch (Exception e) {
\r
175 public final List<JMenuItem> getMenuItems(){
\r
176 List<JMenuItem> ret = new Vector<JMenuItem>();
\r
177 ret.add(new JMenu("New"){
\r
178 private static final long serialVersionUID = 1L;
\r
180 for (final ObjectCreator c : creators ){
\r
181 add(new JMenuItem("New " + c.getName()){
\r
182 private static final long serialVersionUID = 1L;
\r
184 addActionListener(new ActionListener() {
\r
186 public void actionPerformed(ActionEvent ae) {
\r
196 ret.add(new JMenuItem("Open" + noun + "..."){
\r
197 private static final long serialVersionUID = 1L;
\r
199 addActionListener(new ActionListener() {
\r
201 public void actionPerformed(ActionEvent ae) {
\r
202 log.debug("Open...");
\r
208 ret.add(new JMenuItem("Close" + noun){
\r
209 private static final long serialVersionUID = 1L;
\r
211 addActionListener(new ActionListener() {
\r
213 public void actionPerformed(ActionEvent ae) {
\r
214 log.debug("Close");
\r
220 ret.add(new JMenuItem("Save" + noun){
\r
221 private static final long serialVersionUID = 1L;
\r
223 addActionListener(new ActionListener() {
\r
225 public void actionPerformed(ActionEvent ae) {
\r
232 ret.add(new JMenuItem("Save" + noun + " As..."){
\r
233 private static final long serialVersionUID = 1L;
\r
235 addActionListener(new ActionListener() {
\r
237 public void actionPerformed(ActionEvent ae) {
\r
238 log.debug("Save As...");
\r
247 protected final void add(final OBJECT o){
\r
248 EDITOR e = createEditor(o);
\r
249 objectToEditor.put(o, e);
\r
250 editorToObject.put(e, o);
\r
255 public final void load(final File f) throws IOException{
\r
256 OBJECT o = loadFromFile(f);
\r
257 EDITOR e = createEditor(o);
\r
258 objectToEditor.put(o, e);
\r
259 editorToObject.put(e, o);
\r
260 fileToEditor.put(f, e);
\r
261 editorToFile.put(e, f);
\r
262 addTab(f.getName(), e);
\r
265 public abstract EDITOR createEditor(final OBJECT o);
\r
267 public abstract OBJECT newObject();
\r
269 protected abstract OBJECT loadFromFile(final File f) throws IOException;
\r
271 protected abstract void saveToFile(final OBJECT o, final File f) throws IOException;
\r
274 private final void errorDialog(final Throwable t){
\r
275 t.printStackTrace();
\r
276 JOptionPane.showMessageDialog(MultiObjectEditor.this, t.getClass().getSimpleName() + ": " + t.getMessage());
\r