1 23 24 package org.gjt.sp.jedit.options; 25 26 import javax.swing.table.*; 27 import javax.swing.*; 28 import java.awt.event.*; 29 import java.awt.*; 30 import java.util.*; 31 import java.util.List ; 32 33 import org.gjt.sp.jedit.gui.GrabKeyDialog; 34 import org.gjt.sp.jedit.*; 35 import org.gjt.sp.util.Log; 36 import org.gjt.sp.util.StandardUtilities; 37 38 43 public class ShortcutsOptionPane extends AbstractOptionPane 44 { 45 public ShortcutsOptionPane() 46 { 47 super("shortcuts"); 48 } 49 50 protected void _init() 52 { 53 allBindings = new Vector(); 54 55 setLayout(new BorderLayout(12,12)); 56 57 initModels(); 58 59 selectModel = new JComboBox(models); 60 selectModel.addActionListener(new ActionHandler()); 61 selectModel.setToolTipText(jEdit.getProperty("options.shortcuts.select.tooltip")); 62 Box north = Box.createHorizontalBox(); 63 north.add(new JLabel(jEdit.getProperty( 64 "options.shortcuts.select.label"))); 65 north.add(Box.createHorizontalStrut(6)); 66 north.add(selectModel); 67 68 keyTable = new JTable(currentModel); 69 keyTable.getTableHeader().setReorderingAllowed(false); 70 keyTable.getTableHeader().addMouseListener(new HeaderMouseHandler()); 71 keyTable.addMouseListener(new TableMouseHandler()); 72 Dimension d = keyTable.getPreferredSize(); 73 d.height = Math.min(d.height,200); 74 JScrollPane scroller = new JScrollPane(keyTable); 75 scroller.setPreferredSize(d); 76 77 add(BorderLayout.NORTH,north); 78 add(BorderLayout.CENTER,scroller); 79 try { 80 selectModel.setSelectedIndex(jEdit.getIntegerProperty("options.shortcuts.select.index", 0)); 81 } 82 catch (IllegalArgumentException eae) {} 83 } 84 85 protected void _save() 86 { 87 if(keyTable.getCellEditor() != null) 88 keyTable.getCellEditor().stopCellEditing(); 89 90 for (ShortcutsModel model : models) 91 model.save(); 92 93 Macros.loadMacros(); 94 } 95 96 private void initModels() 97 { 98 models = new Vector<ShortcutsModel>(); 99 ActionSet[] actionSets = jEdit.getActionSets(); 100 for(int i = 0; i < actionSets.length; i++) 101 { 102 ActionSet actionSet = actionSets[i]; 103 if(actionSet.getActionCount() != 0) 104 { 105 String modelLabel = actionSet.getLabel(); 106 if(modelLabel == null) 107 { 108 Log.log(Log.ERROR,this,"Empty action set: " 109 + actionSet.getPluginJAR()); 110 } 111 models.addElement(createModel(modelLabel, 112 actionSet.getActionNames())); 113 } 114 } 115 Collections.sort(models,new MiscUtilities.StringICaseCompare()); 116 currentModel = models.elementAt(0); 117 } 118 119 private ShortcutsModel createModel(String modelLabel, String [] actions) 120 { 121 Vector<GrabKeyDialog.KeyBinding[]> bindings = new Vector<GrabKeyDialog.KeyBinding[]>(actions.length); 122 123 for(int i = 0; i < actions.length; i++) 124 { 125 String name = actions[i]; 126 EditAction ea = jEdit.getAction(name); 127 String label = ea.getLabel(); 128 if(label == null) 130 continue; 131 132 label = GUIUtilities.prettifyMenuLabel(label); 133 addBindings(name,label,bindings); 134 } 135 136 return new ShortcutsModel(modelLabel,bindings); 137 } 138 139 private void addBindings(String name, String label, List <GrabKeyDialog.KeyBinding[]> bindings) 140 { 141 GrabKeyDialog.KeyBinding[] b = new GrabKeyDialog.KeyBinding[2]; 142 143 b[0] = createBinding(name,label, 144 jEdit.getProperty(name + ".shortcut")); 145 b[1] = createBinding(name,label, 146 jEdit.getProperty(name + ".shortcut2")); 147 148 bindings.add(b); 149 } 150 151 private GrabKeyDialog.KeyBinding createBinding(String name, 152 String label, String shortcut) 153 { 154 if(shortcut != null && shortcut.length() == 0) 155 shortcut = null; 156 157 GrabKeyDialog.KeyBinding binding 158 = new GrabKeyDialog.KeyBinding(name,label,shortcut,false); 159 160 allBindings.addElement(binding); 161 return binding; 162 } 163 164 private JTable keyTable; 166 private Vector<ShortcutsModel> models; 167 private ShortcutsModel currentModel; 168 private JComboBox selectModel; 169 private Vector<GrabKeyDialog.KeyBinding> allBindings; 170 171 class HeaderMouseHandler extends MouseAdapter 172 { 173 public void mouseClicked(MouseEvent evt) 174 { 175 switch(keyTable.getTableHeader().columnAtPoint(evt.getPoint())) 176 { 177 case 0: 178 currentModel.sort(0); 179 break; 180 case 1: 181 currentModel.sort(1); 182 break; 183 case 2: 184 currentModel.sort(2); 185 break; 186 } 187 } 188 } 189 190 class TableMouseHandler extends MouseAdapter 191 { 192 public void mouseClicked(MouseEvent evt) 193 { 194 int row = keyTable.getSelectedRow(); 195 int col = keyTable.getSelectedColumn(); 196 if(col != 0 && row != -1) 197 { 198 GrabKeyDialog gkd = new GrabKeyDialog( 199 GUIUtilities.getParentDialog( 200 ShortcutsOptionPane.this), 201 currentModel.getBindingAt(row,col-1), 202 allBindings,null); 203 if(gkd.isOK()) 204 currentModel.setValueAt( 205 gkd.getShortcut(),row,col); 206 } 207 } 208 } 209 210 class ActionHandler implements ActionListener 211 { 212 public void actionPerformed(ActionEvent evt) 213 { 214 ShortcutsModel newModel 215 = (ShortcutsModel)selectModel.getSelectedItem(); 216 if(currentModel != newModel) 217 { 218 jEdit.setIntegerProperty("options.shortcuts.select.index", selectModel.getSelectedIndex()); 219 currentModel = newModel; 220 keyTable.setModel(currentModel); 221 } 222 } 223 } 224 225 class ShortcutsModel extends AbstractTableModel 226 { 227 private Vector<GrabKeyDialog.KeyBinding[]> bindings; 228 private String name; 229 230 ShortcutsModel(String name, Vector<GrabKeyDialog.KeyBinding[]> bindings) 231 { 232 this.name = name; 233 this.bindings = bindings; 234 sort(0); 235 } 236 237 public void sort(int col) 238 { 239 Collections.sort(bindings,new KeyCompare(col)); 240 fireTableDataChanged(); 241 } 242 243 public int getColumnCount() 244 { 245 return 3; 246 } 247 248 public int getRowCount() 249 { 250 return bindings.size(); 251 } 252 253 public Object getValueAt(int row, int col) 254 { 255 switch(col) 256 { 257 case 0: 258 return getBindingAt(row,0).label; 259 case 1: 260 return getBindingAt(row,0).shortcut; 261 case 2: 262 return getBindingAt(row,1).shortcut; 263 default: 264 return null; 265 } 266 } 267 268 public void setValueAt(Object value, int row, int col) 269 { 270 if(col == 0) 271 return; 272 273 getBindingAt(row,col-1).shortcut = (String )value; 274 275 fireTableDataChanged(); 278 } 279 280 public String getColumnName(int index) 281 { 282 switch(index) 283 { 284 case 0: 285 return jEdit.getProperty("options.shortcuts.name"); 286 case 1: 287 return jEdit.getProperty("options.shortcuts.shortcut1"); 288 case 2: 289 return jEdit.getProperty("options.shortcuts.shortcut2"); 290 default: 291 return null; 292 } 293 } 294 295 public void save() 296 { 297 for (GrabKeyDialog.KeyBinding[] binding : bindings) 298 { 299 jEdit.setProperty( 300 binding[0].name + ".shortcut", 301 binding[0].shortcut); 302 jEdit.setProperty( 303 binding[1].name + ".shortcut2", 304 binding[1].shortcut); 305 } 306 } 307 308 public GrabKeyDialog.KeyBinding getBindingAt(int row, int nr) 309 { 310 GrabKeyDialog.KeyBinding[] binding = bindings.elementAt(row); 311 return binding[nr]; 312 } 313 314 public String toString() 315 { 316 return name; 317 } 318 319 class KeyCompare implements Comparator 320 { 321 int col; 322 323 KeyCompare(int col) 324 { 325 this.col = col; 326 } 327 328 public int compare(Object obj1, Object obj2) 329 { 330 GrabKeyDialog.KeyBinding[] k1 331 = (GrabKeyDialog.KeyBinding[])obj1; 332 GrabKeyDialog.KeyBinding[] k2 333 = (GrabKeyDialog.KeyBinding[])obj2; 334 335 String label1 = k1[0].label.toLowerCase(); 336 String label2 = k2[0].label.toLowerCase(); 337 338 if(col == 0) 339 return StandardUtilities.compareStrings( 340 label1,label2,true); 341 else 342 { 343 String shortcut1, shortcut2; 344 if(col == 1) 345 { 346 shortcut1 = k1[0].shortcut; 347 shortcut2 = k2[0].shortcut; 348 } 349 else 350 { 351 shortcut1 = k1[1].shortcut; 352 shortcut2 = k2[1].shortcut; 353 } 354 355 if(shortcut1 == null && shortcut2 != null) 356 return 1; 357 else if(shortcut2 == null && shortcut1 != null) 358 return -1; 359 else if(shortcut1 == null) 360 return StandardUtilities.compareStrings(label1,label2,true); 361 else 362 return StandardUtilities.compareStrings(shortcut1,shortcut2,true); 363 } 364 } 365 } 366 } 367 } 368 | Popular Tags |