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 private final Frame frame;
\r
32 private final String noun;
\r
34 private final Map<OBJECT, EDITOR> objectToEditor = new HashMap<OBJECT, EDITOR>();
\r
35 private final Map<EDITOR, OBJECT> editorToObject = new HashMap<EDITOR, OBJECT>();
\r
36 private final Map<File, EDITOR> fileToEditor = new HashMap<File, EDITOR>();
\r
37 private final Map<EDITOR, File> editorToFile = new HashMap<EDITOR, File>();
\r
39 private final Set<OBJECT> dirty = new HashSet<OBJECT>();
\r
41 public MultiObjectEditor(final Frame frame, final String noun){
\r
43 this.noun = " " + noun.trim();
\r
46 public final void dirty(final OBJECT o){
\r
47 if ( !dirty.contains(o) )
\r
48 setTitleAt(indexOfComponent(objectToEditor.get(o)), "*" + getTitleAt(indexOfComponent(objectToEditor.get(o))));
\r
52 public final void dirty(final EDITOR e){
\r
53 dirty(editorToObject.get(e));
\r
56 private final void undirty(final OBJECT o){
\r
57 if ( dirty.contains(o) )
\r
58 setTitleAt(indexOfComponent(objectToEditor.get(o)), getTitleAt(indexOfComponent(objectToEditor.get(o))).replaceAll("^\\*", ""));
\r
62 public final JMenu getMenu(){
\r
63 JMenu ret = new JMenu("File");
\r
64 for ( JMenuItem i : getMenuItems() )
\r
69 private void menuNew(){
\r
73 @SuppressWarnings("unchecked")
\r
75 public EDITOR getSelectedComponent(){
\r
76 return (EDITOR)super.getSelectedComponent();
\r
79 private void close(){
\r
80 EDITOR e = getSelectedComponent();
\r
81 OBJECT o = editorToObject.get(e);
\r
82 File f = editorToFile.get(e);
\r
84 if ( dirty.contains(o) ){
\r
85 int response = JOptionPane.showConfirmDialog(this, "Object is unsaved. Save Before Closing?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
\r
86 if ( response == JOptionPane.YES_OPTION ){
\r
88 } else if ( response == JOptionPane.CANCEL_OPTION ){
\r
93 objectToEditor.remove(o);
\r
94 editorToObject.remove(e);
\r
95 fileToEditor.remove(f);
\r
96 editorToFile.remove(e);
\r
100 private void saveDialog(){
\r
101 EDITOR e = getSelectedComponent();
\r
102 if ( !editorToFile.containsKey(e) ){
\r
103 log.debug("Editor has no file, saving as...");
\r
107 File file = editorToFile.get(e);
\r
108 log.debug("Saving to " + file.getAbsolutePath());
\r
110 saveToFile(editorToObject.get(e), file);
\r
111 undirty(editorToObject.get(e));
\r
112 } catch (IOException e1) {
\r
116 private void saveAsDialog(){
\r
117 EDITOR e = getSelectedComponent();
\r
118 final FileDialog fd = new FileDialog(frame, "Save" + noun + " As", FileDialog.SAVE);
\r
119 fd.setVisible(true);
\r
120 if (fd.getFile() != null ) {
\r
121 File file = new File(fd.getDirectory() + fd.getFile());
\r
123 OBJECT o = editorToObject.get(e);
\r
124 saveToFile(o, file);
\r
126 objectToEditor.put(o, e);
\r
127 editorToObject.put(e, o);
\r
128 fileToEditor.put(file, e);
\r
129 editorToFile.put(e, file);
\r
131 getSelectedIndex(),
\r
133 } catch (Exception e1) {
\r
139 private void openDialog(){
\r
140 final FileDialog fd = new FileDialog(frame, "Open" + noun + "...", FileDialog.LOAD);
\r
141 fd.setVisible(true);
\r
142 if ( fd.getFile() != null ) {
\r
143 File file = new File(fd.getDirectory() + fd.getFile());
\r
144 log.debug("Opening File " + file.getAbsolutePath());
\r
145 if ( fileToEditor.containsKey(file) ){
\r
146 log.debug("File " + file.getAbsolutePath() + "Already open, focusing");
\r
147 setSelectedComponent(fileToEditor.get(file));
\r
151 OBJECT o = loadFromFile(file);
\r
152 EDITOR e = createEditor(o);
\r
153 objectToEditor.put(o, e);
\r
154 editorToObject.put(e, o);
\r
155 fileToEditor.put(file, e);
\r
156 editorToFile.put(e, file);
\r
157 addTab(file.getName(), e);
\r
158 } catch (Exception e) {
\r
164 public final List<JMenuItem> getMenuItems(){
\r
165 List<JMenuItem> ret = new Vector<JMenuItem>();
\r
166 ret.add(new JMenuItem("New" + noun){
\r
167 private static final long serialVersionUID = 1L;
\r
169 addActionListener(new ActionListener() {
\r
171 public void actionPerformed(ActionEvent ae) {
\r
178 ret.add(new JMenuItem("Open" + noun + "..."){
\r
179 private static final long serialVersionUID = 1L;
\r
181 addActionListener(new ActionListener() {
\r
183 public void actionPerformed(ActionEvent ae) {
\r
184 log.debug("Open...");
\r
190 ret.add(new JMenuItem("Close" + noun){
\r
191 private static final long serialVersionUID = 1L;
\r
193 addActionListener(new ActionListener() {
\r
195 public void actionPerformed(ActionEvent ae) {
\r
196 log.debug("Close");
\r
202 ret.add(new JMenuItem("Save" + noun){
\r
203 private static final long serialVersionUID = 1L;
\r
205 addActionListener(new ActionListener() {
\r
207 public void actionPerformed(ActionEvent ae) {
\r
214 ret.add(new JMenuItem("Save" + noun + " As..."){
\r
215 private static final long serialVersionUID = 1L;
\r
217 addActionListener(new ActionListener() {
\r
219 public void actionPerformed(ActionEvent ae) {
\r
220 log.debug("Save As...");
\r
229 protected final void add(final OBJECT o){
\r
230 EDITOR e = createEditor(o);
\r
231 objectToEditor.put(o, e);
\r
232 editorToObject.put(e, o);
\r
237 public final void load(final File f) throws IOException{
\r
238 OBJECT o = loadFromFile(f);
\r
239 EDITOR e = createEditor(o);
\r
240 objectToEditor.put(o, e);
\r
241 editorToObject.put(e, o);
\r
242 fileToEditor.put(f, e);
\r
243 editorToFile.put(e, f);
\r
244 addTab(f.getName(), e);
\r
247 public abstract EDITOR createEditor(final OBJECT o);
\r
249 public abstract OBJECT newObject();
\r
251 protected abstract OBJECT loadFromFile(final File f) throws IOException;
\r
253 protected abstract void saveToFile(final OBJECT o, final File f) throws IOException;
\r
256 private final void errorDialog(final Throwable t){
\r
257 t.printStackTrace();
\r
258 JOptionPane.showMessageDialog(MultiObjectEditor.this, t.getClass().getSimpleName() + ": " + t.getMessage());
\r