001: package tide.editor;
002:
003: import java.util.*;
004: import javax.swing.*;
005: import javax.swing.event.*;
006: import java.awt.event.*;
007: import snow.utils.storage.*;
008: import java.io.*;
009:
010: /** Contains the user auto replacements.
011: * TODO: use a special char to show where the caret goes, for example (*),
012: * broken: the actual indenting breaks the offset. Maybe corrected, must count the number
013: * of lines n back to the pos in the text and subtract n*#spaces to the pos...
014: */
015: public final class UserAutoReplacements {
016: final Hashtable<String, AutoReplacement> replacements = new Hashtable<String, AutoReplacement>();
017:
018: static UserAutoReplacements instance = null;
019:
020: final public JCheckBoxMenuItem enableAutoReplacements = new JCheckBoxMenuItem(
021: "Enable auto replacements", true);
022:
023: private UserAutoReplacements() {
024: load();
025: }
026:
027: // global indent will be generated automatically ! (TODO: shift also)
028: public void createDefaults() {
029: //NO, let user ones,but overwrite the defaults
030: //replacements.clear();
031: add(new AutoReplacement(
032: "nacli",
033: "new ActionListener() { public void actionPerformed(ActionEvent ae) {\n \n} }",
034: 4));
035: add(new AutoReplacement("ntpr",
036: "new Throwable().printStackTrace();", 0));
037: add(new AutoReplacement("prstr", "printStackTrace();", 0));
038: add(new AutoReplacement("sysop", "System.out.println(\"\");", 3));
039: add(new AutoReplacement("syserr", "System.err.println(\"\");",
040: 3));
041: add(new AutoReplacement(
042: "trycaf",
043: "try{\n}\ncatch(Exception e) {\n throw e;\n}\nfinally{\n}",
044: 0));
045: add(new AutoReplacement(
046: "trycat",
047: "try{\n}\ncatch(Exception e) {\n e.printStackTrace();\n}",
048: 0));
049: add(new AutoReplacement("catchefi",
050: "catch(Exception e) {\n throw e;\n}\nfinally{\n}", 0));
051: add(new AutoReplacement("catchex",
052: "catch(Exception e) {\n throw e;\n}", 0));
053: add(new AutoReplacement(
054: "catchprop",
055: "catch(Exception e) {\n throw new RuntimeException(e);\n}",
056: 0));
057: add(new AutoReplacement(
058: "importswings",
059: "import javax.swing.*;\nimport javax.swing.event.*;\nimport java.awt.BorderLayout;\nimport java.awt.event.*;\n",
060: 0));
061: add(new AutoReplacement("staticmain",
062: "public static void main(String[] arguments)\n{\n}", 2));
063: add(new AutoReplacement(
064: "edtlater",
065: "EventQueue.invokeLater(new Runnable() { public void run() {\n \n}});",
066: 5));
067: add(new AutoReplacement(
068: "edtwait",
069: "EventQueue.invokeAndWait(new Runnable() { public void run() {\n \n}});",
070: 5));
071: add(new AutoReplacement(
072: "newthread",
073: "Thread t = new Thread()\n{\n public void run()\n {\n \n }\n };\nt.start();",
074: 20));
075: add(new AutoReplacement(
076: "jopterror",
077: "JOptionPane.showMessageDialog(null, \"blabla\", \"Error\", JOptionPane.ERROR_MESSAGE);",
078: 0));
079: add(new AutoReplacement(
080: "joptconf",
081: "int rep = JOptionPane.showConfirmDialog(parentComponent, \"Are you sure ?.\", \"Confirmation\", JOptionPane.YES_NO_CANCEL_OPTION);"
082: + "\r\n//if(rep!=JOptionPane.OK_OPTION) { return; }",
083: 0));
084:
085: add(new AutoReplacement(
086: "joptin",
087: "String rep = JOptionPane.showInputDialog(TopFrame.this, \"Please enter your name...\", \"Name input\", JOptionPane.QUESTION_MESSAGE);",
088: 0));
089:
090: add(new AutoReplacement(
091: "sleep100",
092: "try{ Thread.sleep(100); } catch(InterruptedException ex) { break; }",
093: 0));
094: add(new AutoReplacement(
095: "mousad",
096: "new MouseAdapter(){\n @Override public void mousePressed(MouseEvent me)\n {\n }"
097: + "\n @Override public void mouseReleased(MouseEvent me)\n {\n }\n}",
098: 3));
099:
100: add(new AutoReplacement(
101: "kadap",
102: "addKeyListener(new KeyAdapter()\n{\n @Override public void keyPressed(KeyEvent ke)\n {\n \n }\n});",
103: 10));
104: add(new AutoReplacement(
105: "wlisterm",
106: "addWindowListener(new WindowAdapter()\n{ \n@Override public void windowClosed(WindowEvent we)"
107: + " {\n terminate();\n }\n @Override public void windowClosing(WindowEvent we)\n { terminate();\n }"
108: + "\n void terminate()\n {\n // System.exit(0); ?\n }\n});",
109: 0));
110: add(new AutoReplacement("flowpan",
111: "new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));", 0));
112: add(new AutoReplacement(
113: "boxpan",
114: "JPanel pan = new JPanel();\r\npan.setLayout(new BoxLayout(pan, BoxLayout.X_AXIS));",
115: 0));
116: add(new AutoReplacement("borpan",
117: "new JPanel(new BorderLayout())", 0));
118: add(new AutoReplacement("borcen", "BorderLayout.CENTER", 0));
119: add(new AutoReplacement(
120: "tostrmet",
121: "@Override public final String toString()\n{\n StringBuilder sb = new StringBuilder();\n //...\n return sb.toString();\n}",
122: 25));
123: add(new AutoReplacement("stribu",
124: "StringBuilder sb = new StringBuilder();\n", 0));
125: add(new AutoReplacement("prifa", "private final ", 0));
126:
127: add(new AutoReplacement(
128: "goodlook",
129: "try {\n \n//UIManager.setLookAndFeel(\"com.jgoodies.looks.windows.WindowsLookAndFeel\");"
130: + "\n UIManager.setLookAndFeel(\"com.jgoodies.looks.plastic.Plastic3DLookAndFeel\");"
131: + "\n //UIManager.setLookAndFeel(\"com.jgoodies.looks.plastic.PlasticLookAndFeel\");"
132: + "\n //UIManager.setLookAndFeel(\"com.jgoodies.looks.plastic.PlasticXPLookAndFeel\");"
133: + "\n} catch (Exception e) { e.printStackTrace(); }",
134: 0));
135: }
136:
137: private void add(AutoReplacement ar) {
138: replacements.put(ar.shortcut, ar);
139: }
140:
141: public AutoReplacement addNew() {
142: AutoReplacement ra = new AutoReplacement("?-?-?-?", "", 0);
143: replacements.put(ra.shortcut, ra);
144: return ra;
145: }
146:
147: /** null if none.
148: */
149: public AutoReplacement getReplacement(String shortcut) {
150: return replacements.get(shortcut);
151: }
152:
153: public boolean hasReplacement(String shortcut) {
154: return replacements.containsKey(shortcut);
155: }
156:
157: public static UserAutoReplacements getInstance() {
158: if (instance == null)
159: instance = new UserAutoReplacements();
160: return instance;
161: }
162:
163: public JMenu createMenu() {
164: JMenu menu = new JMenu("Auto replacements");
165: Set<String> keys = replacements.keySet();
166:
167: JMenuItem manage = new JMenuItem("Manage autoreplacements");
168: menu.add(manage);
169: menu.addSeparator();
170: menu.add(enableAutoReplacements);
171: menu.addSeparator();
172: manage.addActionListener(new ActionListener() {
173: public void actionPerformed(ActionEvent ae) {
174: new AutoReplaceManagementDialog();
175: }
176: });
177:
178: for (String key : keys) {
179: final String fkey = key;
180: JMenuItem mi = new JMenuItem(key);
181: menu.add(mi);
182: mi.addActionListener(new ActionListener() {
183: public void actionPerformed(ActionEvent ae) {
184: if (!MainEditorFrame.instance.editorPanel.textPane
185: .isEditable())
186: return;
187:
188: MainEditorFrame.instance.editorPanel
189: .writeTextAtCaret(replacements.get(fkey).replacement);
190: }
191: });
192: }
193:
194: return menu;
195: }
196:
197: // Persistence
198: //
199:
200: public void save() {
201: File file = new File(System.getProperty("user.home"),
202: ".tide_global/tide_autoreplacements.vec");
203: try {
204: FileUtils.saveVectorToFile(file, getVectorRepresentation());
205: } catch (Exception e) {
206: e.printStackTrace();
207: }
208: }
209:
210: public void load() {
211: File file = new File(System.getProperty("user.home"),
212: ".tide_global/tide_autoreplacements.vec");
213: File oldFile = new File(System.getProperty("user.home"),
214: "tide_autoreplacements.vec");
215: if (oldFile.exists()) {
216: try {
217: FileUtils.copy(oldFile, file);
218: if (!oldFile.delete())
219: oldFile.deleteOnExit();
220: } catch (Exception e) {
221: e.printStackTrace();
222: }
223: }
224:
225: if (file.exists()) {
226: try {
227: this .createFromVectorRepresentation(FileUtils
228: .loadVectorFromFile(file));
229: } catch (Exception e) {
230: e.printStackTrace();
231: createDefaults();
232: }
233: } else {
234: createDefaults();
235: }
236: }
237:
238: public List<Object> getVectorRepresentation() {
239: List<Object> rep = new ArrayList<Object>();
240: rep.add(1); //0
241: List<Object> aro = new ArrayList<Object>();
242: rep.add(aro); //1
243: Iterator<AutoReplacement> it = this .replacements.values()
244: .iterator();
245: while (it.hasNext()) {
246: aro.add(it.next().getVectorRepresentation());
247: }
248: return rep;
249: }
250:
251: @SuppressWarnings("unchecked")
252: public void createFromVectorRepresentation(List<Object> rep) {
253: this .replacements.clear();
254: List<Object> aro = (ArrayList<Object>) rep.get(1);
255: for (int i = 0; i < aro.size(); i++) {
256: add(new AutoReplacement((ArrayList<Object>) aro.get(i)));
257: }
258:
259: }
260:
261: /** A single replacement definition
262: */
263: public static class AutoReplacement {
264: public String shortcut;
265: private String replacement;
266: public int rewindAfterReplace;
267:
268: public String getReplacement() {
269: return replacement;
270: }
271:
272: public void setReplacement(String r) {
273: replacement = r;
274: }
275:
276: public AutoReplacement(String shortcut, String replacement,
277: int rewindAfterReplace) {
278: this .shortcut = shortcut;
279: this .replacement = replacement;
280: this .rewindAfterReplace = rewindAfterReplace;
281: }
282:
283: public List<Object> getVectorRepresentation() {
284: List<Object> rep = new Vector<Object>();
285: rep.add(1);
286: rep.add(shortcut);
287: rep.add(replacement);
288: rep.add(rewindAfterReplace);
289:
290: return rep;
291: }
292:
293: public AutoReplacement(List<Object> rep) {
294: createFromVectorRepresentation(rep);
295: }
296:
297: public void createFromVectorRepresentation(List<Object> rep) {
298: shortcut = (String) rep.get(1);
299: replacement = (String) rep.get(2);
300: rewindAfterReplace = (Integer) rep.get(3);
301: }
302: }
303:
304: }
|