View Javadoc
1 2 //import com.riege.intern.FileLocator; 3 //import java.awt.*; 4 package net.sf.plb4jedit.filelocator; 5 import java.awt.BorderLayout; 6 import java.awt.FlowLayout; 7 import java.awt.GridLayout; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 import java.awt.event.FocusEvent; 11 import java.awt.event.FocusListener; 12 import java.util.Enumeration; 13 import java.util.Vector; 14 15 import javax.swing.*; 16 import javax.swing.border.EmptyBorder; 17 18 import org.gjt.sp.jedit.*; 19 import org.gjt.sp.util.Log; 20 /*** 21 * Description of the Class 22 * 23 * @author schaefer 24 * @created April 25, 2002 25 */ 26 public class FileLocatorPlugin extends EditPlugin { 27 28 private static FileLocator locator; 29 public final static String PROPERTY_PREFIX = "plugin.filelocator."; 30 31 /*** Description of the Method */ 32 public void start() { 33 34 } 35 36 private static void initializeLocator() { 37 if (locator == null) { 38 try { 39 String mapName = jEdit.getProperty(PROPERTY_PREFIX + "mapname"); 40 if (mapName == null || mapName.length() == 0) { 41 locator = new FileLocator(); 42 } else { 43 locator = new FileLocator(mapName); 44 } 45 46 } catch (Exception e) { 47 //Log.log( 48 } 49 } 50 } 51 52 /*** 53 * Description of the Method 54 * 55 * @param menuItems param 56 */ 57 public void createMenuItems(Vector menuItems) { 58 menuItems.addElement(GUIUtilities.loadMenu("filelocator.menu")); 59 } 60 61 /*** 62 * Description of the Method 63 * 64 * @param dialog param 65 */ 66 //public void createOptionPanes(OptionsDialog dialog) { 67 // dialog.addOptionPane(new FileLocatorOptionPane(options)); 68 //} 69 70 /*** 71 * Description of the Method 72 * 73 * @param view param 74 */ 75 public static void dump(View view) { 76 initializeLocator(); 77 Buffer dump = jEdit.newFile(view); 78 String s = null; 79 try { 80 s = locator.dump(); 81 } catch (Exception e) { 82 // Log.log() 83 dump.insert(0, e.getMessage()); 84 return; 85 } 86 dump.insert(0, s); 87 } 88 89 /*** 90 * Description of the Method 91 * 92 * @param buffer param 93 * @param view param 94 * @param word param 95 * @return 0 : load again, 1 = loaded or cancelled, -1 error 96 */ 97 public static int load( 98 Buffer buffer, 99 View view, 100 String word, 101 String mode) { 102 initializeLocator(); 103 String[] paths = null; 104 if (mode == null || mode.length() == 0) { 105 view.getStatus().setMessageAndClear("No mode selected."); 106 } 107 if (word == null || word.length() == 0) { 108 view.getStatus().setMessageAndClear("No word selected"); 109 } 110 try { 111 view.getStatus().setMessageAndClear( 112 "search in map " + mode + " for '" + word + "'"); 113 paths = locator.locate(mode, word); 114 } catch (Exception e) { 115 view.getStatus().setMessageAndClear( 116 "partial search in map " + mode + " for '" + word + "'"); 117 try { 118 paths = locator.locatePartially(mode, word); 119 } catch (Exception e2) { 120 view.getStatus().setMessageAndClear(e2.getMessage()); 121 return -1; 122 } 123 } 124 if (paths == null || paths.length == 0) { 125 String message = 126 "'" + word + "' not found in map '" + mode + "' Search again ?"; 127 view.getStatus().setMessageAndClear(message); 128 return javax.swing.JOptionPane.showConfirmDialog( 129 view, 130 message, 131 "Confirm : ", 132 javax.swing.JOptionPane.YES_NO_OPTION); 133 } else if (paths.length == 1) { 134 jEdit.openFile(view, paths[0]); 135 return 1; 136 } else { 137 FileSelector select = new FileSelector(paths, view); 138 String fileName = select.getFileName(); 139 if (select.isCancelled()) { 140 view.getStatus().setMessageAndClear("Cancelled"); 141 return 1; 142 } else if (select.newSearch()) { 143 return 0; 144 } else if (fileName.length() == 0) { 145 view.getStatus().setMessageAndClear( 146 "fileName from FileSelector empty"); 147 return 1; 148 } else { 149 view.getStatus().setMessageAndClear("open : " + fileName); 150 jEdit.openFile(view, fileName); 151 return 1; 152 } 153 } 154 } 155 156 /*** 157 * Description of the Method 158 * 159 * @param buffer param 160 * @param view param 161 */ 162 public static void load(Buffer buffer, View view) { 163 initializeLocator(); 164 int doit = 0; 165 String word = null; 166 String mode = null; 167 while (doit <= 0) { 168 169 FileLocatorLoadDialog inputDialog = 170 new FileLocatorLoadDialog( 171 buffer, 172 view, 173 locator.getMaps(), 174 word, 175 mode); 176 if (inputDialog.isCancelled()) { 177 doit = 1; 178 } else { 179 word = inputDialog.getFileName(); 180 mode = inputDialog.getMode(); 181 doit = load(buffer, view, word, mode); 182 } 183 } 184 } 185 186 /*** Description of the Method */ 187 public static void reload() { 188 initializeLocator(); 189 try { 190 Log.log(Log.DEBUG, locator, "Reload map"); 191 locator.reload(); 192 } catch (Exception e) { 193 //Log.log 194 } 195 } 196 197 /*** Description of the Method */ 198 public static void delete() { 199 initializeLocator(); 200 locator.delete(); 201 } 202 203 /*** 204 * update all maps configured in the options. 205 * this deletes the configured maps and creates them from scratch! 206 */ 207 public static void update() { 208 initializeLocator(); 209 new Thread() { 210 public void run() { 211 Vector mapConfigs = 212 FileLocatorOptions.getInstance().getMapConfigs(); 213 // 1. delete old stuff 214 String[] currentMaps = locator.getMaps(); 215 if (currentMaps != null) { 216 for (int i = 0; i < currentMaps.length; i++) { 217 locator.delete(currentMaps[i]); 218 Log.log( 219 Log.DEBUG, 220 locator, 221 "Deleting map " + currentMaps[i]); 222 } 223 } 224 Enumeration mapConfigsEnum = mapConfigs.elements(); 225 while (mapConfigsEnum.hasMoreElements()) { 226 MapConfig mapConfig = 227 (MapConfig) mapConfigsEnum.nextElement(); 228 String name = mapConfig.getName(); 229 String directory = mapConfig.getDirectory(); 230 String extension = mapConfig.getExtension(); 231 Log.log( 232 Log.DEBUG, 233 locator, 234 "Updating map " 235 + name 236 + ", " 237 + directory 238 + ", " 239 + extension); 240 try { 241 locator.insertMap(name, directory, extension); 242 } catch (Exception e) { 243 Log.log(Log.ERROR, locator, e); 244 } 245 } 246 reload(); 247 } 248 }; 249 } 250 251 } 252 253 /*** 254 * Description of the Class 255 * 256 * @author schaefer 257 * @created April 25, 2002 258 */ 259 class FileSelector implements ActionListener { 260 261 private String fileName; 262 private JDialog dialog; 263 private JComboBox jcb; 264 private JButton ok; 265 private JButton cancel; 266 private JButton newSearch; 267 private String result; 268 269 public static final String CANCELLED = "CANCELLED"; 270 public static final String OK = "OK"; 271 public static final String NEWSEARCH = "NEWSEARCH"; 272 273 /*** 274 *Constructor for the FileSelector object 275 * 276 * @param paths param 277 * @param view param 278 */ 279 public FileSelector(String[] paths, View view) { 280 fileName = null; 281 result = null; 282 // create dialog object and set its features 283 String title = "Select one file"; 284 dialog = new JDialog(view, title, true); 285 JPanel content = new JPanel(new BorderLayout()); 286 content.setBorder(new EmptyBorder(12, 12, 12, 12)); 287 dialog.setContentPane(content); 288 ok = new JButton("OK"); 289 ok.addActionListener(this); 290 cancel = new JButton("Cancel"); 291 cancel.addActionListener(this); 292 newSearch = new JButton("new Search"); 293 newSearch.addActionListener(this); 294 jcb = new JComboBox(paths); 295 jcb.setSelectedIndex(0); 296 JPanel jcbPanel = new JPanel(new BorderLayout()); 297 jcbPanel.add(jcb, BorderLayout.NORTH); 298 content.add(jcbPanel, BorderLayout.CENTER); 299 JPanel buttonPanel = new JPanel(new GridLayout(1, 3)); 300 buttonPanel.add(ok); 301 buttonPanel.add(cancel); 302 buttonPanel.add(newSearch); 303 JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 304 southPanel.add(buttonPanel); 305 content.add(southPanel, BorderLayout.SOUTH); 306 dialog.getRootPane().setDefaultButton(ok); 307 dialog.pack(); 308 dialog.setLocationRelativeTo(view); 309 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 310 dialog.setVisible(true); 311 } 312 313 /*** 314 * Description of the Method 315 * 316 * @param e param 317 */ 318 public void actionPerformed(ActionEvent e) { 319 320 if (e.getSource() == ok) { 321 this.fileName = (String) jcb.getSelectedItem(); 322 this.result = OK; 323 dialog.dispose(); 324 } else if (e.getSource() == cancel) { 325 this.fileName = null; 326 this.result = CANCELLED; 327 dialog.dispose(); 328 } else if (e.getSource() == newSearch) { 329 this.fileName = null; 330 this.result = NEWSEARCH; 331 dialog.dispose(); 332 } 333 } 334 335 public boolean newSearch() { 336 return result.equals(NEWSEARCH); 337 } 338 339 public boolean isCancelled() { 340 return result.equals(CANCELLED); 341 } 342 343 /*** 344 * Gets the fileName attribute of the FileSelector object 345 * 346 * @return The fileName value 347 */ 348 public String getFileName() { 349 return fileName; 350 } 351 } 352 353 /*** 354 * Description of the Class 355 * 356 * @author schaefer 357 * @created April 25, 2002 358 */ 359 class FileLocatorLoadDialog implements ActionListener, FocusListener { 360 private Buffer buffer; 361 private View view; 362 private String fileName; 363 private String mode; 364 private JDialog dialog; 365 private JComboBox modes; 366 private JButton ok; 367 private JButton cancel; 368 private JTextField fileNameField; 369 private boolean isCancelled; 370 371 /*** 372 *Constructor for the FileSelector object 373 * 374 * @param paths param 375 * @param view param 376 */ 377 public FileLocatorLoadDialog( 378 Buffer buffer, 379 View view, 380 String[] modeNames, 381 String defaultSearch, 382 String defaultMode) { 383 this.buffer = buffer; 384 this.view = view; 385 fileName = null; 386 isCancelled = false; 387 mode = null; 388 if (modeNames == null || modeNames.length == 0) { 389 Macros.error(view, "No Maps defined in FileLocator."); 390 isCancelled = true; 391 return; 392 } 393 // create dialog object and set its features 394 String title = "Enter filename:"; 395 dialog = new JDialog(view, title, true); 396 JPanel content = new JPanel(new BorderLayout()); 397 content.setBorder(new EmptyBorder(12, 12, 12, 12)); 398 dialog.setContentPane(content); 399 ok = new JButton("OK"); 400 ok.addActionListener(this); 401 cancel = new JButton("Cancel"); 402 cancel.addActionListener(this); 403 String actualModeName = buffer.getMode().getName(); 404 if (defaultMode != null) { 405 actualModeName = defaultMode; 406 } 407 int bufferModeIndex = 0; 408 for (int i = 0; i < modeNames.length; i++) { 409 if (actualModeName.equals(modeNames[i])) { 410 bufferModeIndex = i; 411 } 412 } 413 fileNameField = new JTextField(); 414 fileNameField.addFocusListener(this); 415 if (defaultSearch != null) { 416 fileNameField.setText(defaultSearch); 417 } 418 modes = new JComboBox(modeNames); 419 modes.setSelectedIndex(bufferModeIndex); 420 JPanel modesPanel = new JPanel(new GridLayout(1, 2)); 421 modesPanel.add(fileNameField); 422 modesPanel.add(modes); 423 content.add(modesPanel, BorderLayout.CENTER); 424 JPanel buttonPanel = new JPanel(new GridLayout(1, 2)); 425 buttonPanel.add(ok); 426 buttonPanel.add(cancel); 427 JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 428 southPanel.add(buttonPanel); 429 content.add(southPanel, BorderLayout.SOUTH); 430 dialog.getRootPane().setDefaultButton(ok); 431 dialog.pack(); 432 dialog.setLocationRelativeTo(view); 433 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 434 dialog.setVisible(true); 435 } 436 437 /*** 438 * Description of the Method 439 * 440 * @param e param 441 */ 442 public void actionPerformed(ActionEvent e) { 443 if (e.getSource() == ok) { 444 this.mode = (String) modes.getSelectedItem(); 445 this.fileName = fileNameField.getText(); 446 isCancelled = false; 447 dialog.dispose(); 448 } else if (e.getSource() == cancel) { 449 this.fileName = null; 450 isCancelled = true; 451 dialog.dispose(); 452 453 } 454 } 455 456 public void focusGained(FocusEvent e) { 457 fileNameField.selectAll(); 458 } 459 460 public void focusLost(FocusEvent e) { 461 } 462 463 public String getFileName() { 464 return fileName; 465 } 466 467 public String getMode() { 468 return mode; 469 } 470 471 public boolean isCancelled() { 472 return isCancelled; 473 } 474 }

This page was automatically generated by Maven