1 22 23 package org.gjt.sp.jedit.gui; 24 25 import javax.swing.border.*; 27 import javax.swing.*; 28 import java.awt.event.*; 29 import java.awt.*; 30 import java.util.*; 31 import org.gjt.sp.jedit.*; 32 34 public class EditAbbrevDialog extends JDialog 35 { 36 40 public EditAbbrevDialog(Frame frame, String abbrev, String expansion, 41 Map abbrevs) 42 { 43 super(frame,jEdit.getProperty("edit-abbrev.title"),true); 44 init(abbrev, expansion, abbrevs); 45 } 47 public EditAbbrevDialog(Dialog dialog, String abbrev, String expansion, 49 Map abbrevs) 50 { 51 super(dialog,jEdit.getProperty("edit-abbrev.title"),true); 52 init(abbrev, expansion, abbrevs); 53 } 55 public String getAbbrev() 57 { 58 if(!isOK) 59 return null; 60 61 return editor.getAbbrev(); 62 } 64 public String getExpansion() 66 { 67 if(!isOK) 68 return null; 69 70 return editor.getExpansion(); 71 } 73 private AbbrevEditor editor; 75 private JButton ok; 76 private JButton cancel; 77 private boolean isOK; 78 private String originalAbbrev; 79 private Map abbrevs; 80 81 private void init(String abbrev, String expansion, Map abbrevs) 83 { 84 this.abbrevs = abbrevs; 85 86 this.originalAbbrev = abbrev; 87 88 JPanel content = new JPanel(new BorderLayout()); 89 content.setBorder(new EmptyBorder(12,12,12,12)); 90 setContentPane(content); 91 92 editor = new AbbrevEditor(); 93 editor.setAbbrev(abbrev); 94 editor.setExpansion(expansion); 95 editor.setBorder(new EmptyBorder(0,0,12,0)); 96 content.add(BorderLayout.CENTER,editor); 97 98 Box box = new Box(BoxLayout.X_AXIS); 99 box.add(Box.createGlue()); 100 ok = new JButton(jEdit.getProperty("common.ok")); 101 ok.addActionListener(new ActionHandler()); 102 getRootPane().setDefaultButton(ok); 103 box.add(ok); 104 box.add(Box.createHorizontalStrut(6)); 105 cancel = new JButton(jEdit.getProperty("common.cancel")); 106 cancel.addActionListener(new ActionHandler()); 107 box.add(cancel); 108 box.add(Box.createGlue()); 109 content.add(BorderLayout.SOUTH,box); 110 111 KeyListener listener = new KeyHandler(); 112 addKeyListener(listener); 113 editor.getBeforeCaretTextArea().addKeyListener(listener); 114 editor.getAfterCaretTextArea().addKeyListener(listener); 115 116 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 117 pack(); 118 setLocationRelativeTo(getParent()); 119 setVisible(true); 120 } 122 private boolean checkForExistingAbbrev() 124 { 125 String abbrev = editor.getAbbrev(); 126 if(abbrevs.get(abbrev) != null) 127 { 128 if(abbrev.equals(originalAbbrev)) 129 return true; 130 131 int result = GUIUtilities.confirm(this, 132 "edit-abbrev.duplicate",null, 133 JOptionPane.YES_NO_OPTION, 134 JOptionPane.WARNING_MESSAGE); 135 return (result == JOptionPane.YES_OPTION); 136 } 137 138 return true; 139 } 141 143 class ActionHandler implements ActionListener 145 { 146 public void actionPerformed(ActionEvent evt) 147 { 148 if(evt.getSource() == ok) 149 { 150 if(editor.getAbbrev() == null 151 || editor.getAbbrev().length() == 0) 152 { 153 getToolkit().beep(); 154 return; 155 } 156 157 if(!checkForExistingAbbrev()) 158 return; 159 160 isOK = true; 161 } 162 163 dispose(); 164 } 165 } 167 class KeyHandler extends KeyAdapter 169 { 170 public void keyPressed(KeyEvent evt) 171 { 172 if(evt.getKeyCode() == KeyEvent.VK_ESCAPE) 173 dispose(); 174 } 175 } } 177 | Popular Tags |