1 21 22 package org.armedbear.j; 23 24 import java.awt.Cursor ; 25 import java.awt.Dimension ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.Date ; 29 import java.util.Iterator ; 30 import java.util.Set ; 31 import java.util.Vector ; 32 import javax.swing.BorderFactory ; 33 import javax.swing.Box ; 34 import javax.swing.BoxLayout ; 35 import javax.swing.JComboBox ; 36 import javax.swing.JPanel ; 37 import javax.swing.JTextField ; 38 import javax.swing.border.Border ; 39 40 public final class PropertiesDialog extends AbstractDialog implements Constants 41 { 42 private static final String TEXT_LF = "LF"; 43 private static final String TEXT_CR = "CR"; 44 private static final String TEXT_CRLF = "CR+LF"; 45 46 private final Editor editor; 47 private final Buffer buffer; 48 49 private JComboBox modeComboBox; 50 private JTextField tabWidthTextField; 51 private JTextField indentSizeTextField; 52 private JTextField wrapColumnTextField; 53 private CheckBox useTabsCheckBox; 54 private CheckBox indentBeforeBraceCheckBox; 55 private CheckBox indentAfterBraceCheckBox; 56 private JComboBox lineSeparatorComboBox; 57 58 public PropertiesDialog() 59 { 60 super(Editor.currentEditor(), "Properties", true); 61 62 editor = Editor.currentEditor(); 63 buffer = editor.getBuffer(); 64 final int modeId = buffer.getModeId(); 65 66 final int WIDTH = 30; 67 68 if (!buffer.isUntitled()) { 69 JPanel group = new JPanel (); 70 group.setLayout(new BoxLayout (group, BoxLayout.Y_AXIS)); 71 group.setAlignmentX(LEFT_ALIGNMENT); 72 Border etchedBorder = BorderFactory.createEtchedBorder(); 73 Border titledBorder = BorderFactory.createTitledBorder(etchedBorder, "File"); 74 group.setBorder(titledBorder); 75 76 final File file = buffer.getFile(); 77 StaticTextField fileTextField = 78 new StaticTextField(file.isRemote() ? file.netPath() : 79 file.canonicalPath()); 80 group.add(fileTextField); 81 82 StaticTextField modificationTextField = null; 83 84 if (file.getProtocol() == File.PROTOCOL_HTTP) { 85 if (buffer.getLastModified() != 0) { 86 Date date = new Date (buffer.getLastModified()); 87 modificationTextField = new StaticTextField("Last modified ".concat(date.toString())); 88 } 89 } else if (file.isLocal()) { 90 Date date = new Date (file.lastModified()); 91 FastStringBuffer sb = new FastStringBuffer(); 92 if (buffer.isModified()) 93 sb.append("Modified:"); 94 else 95 sb.append("Not modified;"); 96 sb.append(" last saved "); 97 sb.append(date.toString()); 98 modificationTextField = new StaticTextField(sb.toString()); 99 } 100 101 if (modificationTextField != null) { 102 group.add(Box.createVerticalStrut(6)); 103 group.add(modificationTextField); 104 } 105 106 String encoding = buffer.getSaveEncoding(); 107 if (encoding != null) { 108 group.add(Box.createVerticalStrut(6)); 109 group.add(new StaticTextField(encoding)); 110 } else 111 Debug.bug(); 112 113 buffer.checkCVS(); 114 CVSEntry cvsEntry = buffer.getCVSEntry(); 115 if (cvsEntry != null) { 116 String revision = cvsEntry.getRevision(); 117 if (revision != null && revision.length() > 0) { 118 FastStringBuffer sb = new FastStringBuffer("CVS "); 119 if (revision.equals("0")) { 120 sb.append(" (locally added)"); 121 } else { 122 sb.append(" revision "); 123 sb.append(revision); 124 if (buffer.getLastModified() != cvsEntry.getCheckoutTime()) 125 sb.append(" (locally modified)"); 126 } 127 group.add(Box.createVerticalStrut(6)); 128 group.add(new StaticTextField(sb.toString())); 129 } 130 } 131 132 if (Editor.checkExperimental()) { 133 String s = P4.getStatusString(file); 134 if (s != null) { 135 group.add(Box.createVerticalStrut(6)); 136 group.add(new StaticTextField(s)); 137 } 138 } 139 140 if (buffer.isReadOnly()) { 141 if (file.getProtocol() == File.PROTOCOL_HTTP) 142 ; 143 else if (modeId == BINARY_MODE) 144 ; 145 else if (modeId == IMAGE_MODE) 146 ; 147 else if (modeId == WEB_MODE) 148 ; 149 else { 150 StaticTextField readOnlyTextField = 151 new StaticTextField("File is read only on disk", WIDTH); 152 group.add(Box.createVerticalStrut(6)); 153 group.add(readOnlyTextField); 154 } 155 } 156 157 mainPanel.add(group); 158 } 159 160 if (modeId != IMAGE_MODE && modeId != WEB_MODE){ 161 modeComboBox = new JComboBox (getPermissibleModes()); 163 Dimension dim = modeComboBox.getPreferredSize(); 164 modeComboBox.setMinimumSize(dim); 165 modeComboBox.setMaximumSize(dim); 166 167 JPanel flow = new JPanel (); 168 flow.setLayout(new BoxLayout (flow, BoxLayout.X_AXIS)); 169 flow.setAlignmentX(LEFT_ALIGNMENT); 170 Label label = new Label("Mode:"); 171 flow.add(label); 172 label.setDisplayedMnemonic('M'); 173 174 modeComboBox.setSelectedItem(buffer.getMode().toString()); 175 label.setLabelFor(modeComboBox); 176 modeComboBox.addKeyListener(this); 177 178 flow.add(Box.createHorizontalStrut(5)); 179 flow.add(modeComboBox); 180 181 addVerticalStrut(); 182 mainPanel.add(flow); 183 184 if (modeId != ARCHIVE_MODE && modeId != BINARY_MODE) { 185 flow = new JPanel (); 186 flow.setLayout(new BoxLayout (flow, BoxLayout.X_AXIS)); 187 flow.setAlignmentX(LEFT_ALIGNMENT); 188 label = new Label("Tab width:"); 189 label.setDisplayedMnemonic('T'); 190 flow.add(label); 191 flow.add(Box.createHorizontalStrut(5)); 192 tabWidthTextField = new JTextField (3); 193 dim = tabWidthTextField.getPreferredSize(); 194 tabWidthTextField.setMinimumSize(dim); 195 tabWidthTextField.setMaximumSize(dim); 196 tabWidthTextField.setAlignmentX(LEFT_ALIGNMENT); 197 tabWidthTextField.setText(String.valueOf(buffer.getTabWidth())); 198 label.setLabelFor(tabWidthTextField); 199 flow.add(tabWidthTextField); 200 201 flow.add(Box.createHorizontalStrut(5)); 202 203 label = new Label("Indent size:"); 204 label.setDisplayedMnemonic('I'); 205 flow.add(label); 206 flow.add(Box.createHorizontalStrut(5)); 207 indentSizeTextField = new JTextField (3); 208 dim = indentSizeTextField.getPreferredSize(); 209 indentSizeTextField.setMinimumSize(dim); 210 indentSizeTextField.setMaximumSize(dim); 211 indentSizeTextField.setAlignmentX(LEFT_ALIGNMENT); 212 indentSizeTextField.setText(String.valueOf(buffer.getIndentSize())); 213 label.setLabelFor(indentSizeTextField); 214 flow.add(indentSizeTextField); 215 216 flow.add(Box.createHorizontalStrut(5)); 217 218 label = new Label("Wrap column:"); 219 label.setDisplayedMnemonic('W'); 220 flow.add(label); 221 flow.add(Box.createHorizontalStrut(5)); 222 wrapColumnTextField = new JTextField (3); 223 dim = wrapColumnTextField.getPreferredSize(); 224 wrapColumnTextField.setMinimumSize(dim); 225 wrapColumnTextField.setMaximumSize(dim); 226 wrapColumnTextField.setAlignmentX(LEFT_ALIGNMENT); 227 wrapColumnTextField.setText(String.valueOf(buffer.getIntegerProperty(Property.WRAP_COL))); 228 label.setLabelFor(wrapColumnTextField); 229 flow.add(wrapColumnTextField); 230 231 addVerticalStrut(); 232 mainPanel.add(flow); 233 234 useTabsCheckBox = new CheckBox("Use tabs", buffer.getUseTabs()); 235 useTabsCheckBox.setMnemonic('U'); 236 addVerticalStrut(); 237 addCheckBox(useTabsCheckBox); 238 useTabsCheckBox.setEnabled(true); 239 240 switch (modeId) { 241 case JAVA_MODE: 242 case JAVASCRIPT_MODE: 243 case C_MODE: 244 case CPP_MODE: 245 case PERL_MODE: 246 case TCL_MODE: 247 indentBeforeBraceCheckBox = 248 new CheckBox("Indent before '{'", 249 buffer.getBooleanProperty(Property.INDENT_BEFORE_BRACE)); 250 indentBeforeBraceCheckBox.setMnemonic('b'); 251 addVerticalStrut(); 252 addCheckBox(indentBeforeBraceCheckBox); 253 indentAfterBraceCheckBox = 254 new CheckBox("Indent after '{'", 255 buffer.getBooleanProperty(Property.INDENT_AFTER_BRACE)); 256 indentAfterBraceCheckBox.setMnemonic('a'); 257 addVerticalStrut(); 258 addCheckBox(indentAfterBraceCheckBox); 259 default: 260 break; 261 } 262 263 addVerticalStrut(); 264 265 Vector v = new Vector (); 267 v.add(TEXT_LF); 268 v.add(TEXT_CRLF); 269 v.add(TEXT_CR); 270 lineSeparatorComboBox = new JComboBox (v); 271 dim = lineSeparatorComboBox.getPreferredSize(); 272 lineSeparatorComboBox.setMinimumSize(dim); 273 lineSeparatorComboBox.setMaximumSize(dim); 274 275 flow = new JPanel (); 276 flow.setLayout(new BoxLayout (flow, BoxLayout.X_AXIS)); 277 flow.setAlignmentX(LEFT_ALIGNMENT); 278 label = new Label("Use"); 279 flow.add(label); 280 label.setDisplayedMnemonic('L'); 281 282 if (buffer.lineSeparator == null) 283 buffer.lineSeparator = System.getProperty("line.separator"); 284 285 if (buffer.lineSeparator.equals("\n")) 286 lineSeparatorComboBox.setSelectedItem(TEXT_LF); 287 else if (buffer.lineSeparator.equals("\r\n")) 288 lineSeparatorComboBox.setSelectedItem(TEXT_CRLF); 289 else if (buffer.lineSeparator.equals("\r")) 290 lineSeparatorComboBox.setSelectedItem(TEXT_CR); 291 292 label.setLabelFor(lineSeparatorComboBox); 293 294 flow.add(Box.createHorizontalStrut(5)); 295 flow.add(lineSeparatorComboBox); 296 297 flow.add(Box.createHorizontalStrut(5)); 298 flow.add(new Label("as line separator when saving file")); 299 300 mainPanel.add(flow); 301 } 302 303 addVerticalStrut(); 304 } 305 306 addOKCancel(); 307 308 if (tabWidthTextField != null) 309 tabWidthTextField.addKeyListener(this); 310 311 if (indentSizeTextField != null) 312 indentSizeTextField.addKeyListener(this); 313 314 if (wrapColumnTextField != null) 315 wrapColumnTextField.addKeyListener(this); 316 317 pack(); 318 319 if (tabWidthTextField != null) 320 tabWidthTextField.requestFocus(); 321 } 322 323 private String [] getPermissibleModes() 324 { 325 ModeList modeList = Editor.getModeList(); 326 int fileType = buffer.getFileType(); 327 if (fileType == FILETYPE_ZIP) { 328 String [] array = new String [2]; 329 array[0] = ARCHIVE_MODE_NAME; 330 array[1] = BINARY_MODE_NAME; 331 return array; 332 } 333 if (fileType == FILETYPE_BINARY || fileType == FILETYPE_JPEG) { 334 String [] array; 335 if (Editor.getModeList().modeAccepts(IMAGE_MODE, buffer.getFile().getName())) { 336 array = new String [2]; 337 array[0] = IMAGE_MODE_NAME; 338 array[1] = BINARY_MODE_NAME; 339 } else { 340 array = new String [1]; 341 array[0] = BINARY_MODE_NAME; 342 } 343 return array; 344 } 345 ArrayList list = new ArrayList (); 346 synchronized (modeList) { 347 Iterator it = modeList.iterator(); 348 while (it.hasNext()) { 349 ModeListEntry entry = (ModeListEntry) it.next(); 350 if (entry.isSelectable()) 351 list.add(entry.getDisplayName()); 352 } 353 } 354 return (String []) list.toArray(new String [list.size()]); 355 } 356 357 private boolean save() 358 { 359 if (modeComboBox != null) { 360 String modeName = (String ) modeComboBox.getSelectedItem(); 361 if (modeName != null && !modeName.equals(buffer.getModeName())) { 362 Mode newMode = Editor.getModeList().getModeFromModeName(modeName); 363 if (buffer.getModeId() == BINARY_MODE || newMode.getId() == BINARY_MODE) { 364 if (buffer.isModified()) { 365 FastStringBuffer sb = new FastStringBuffer("Buffer will be reloaded in "); 366 if (newMode.getId() == BINARY_MODE) 367 sb.append("binary"); 368 else 369 sb.append(newMode.toString()); 370 sb.append(" mode; discard changes?"); 371 boolean confirmed = editor.confirm(buffer.getFile().getName(), sb.toString()); 372 if (!confirmed) 373 return false; 374 } 375 } 376 paint(getGraphics()); 377 editor.repaintNow(); 378 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 379 editor.setWaitCursor(); 380 buffer.changeMode(newMode); 381 editor.setDefaultCursor(); 382 setCursor(Cursor.getDefaultCursor()); 383 } 384 } 385 boolean error = false; 386 if (tabWidthTextField != null) { 387 try { 388 String s = tabWidthTextField.getText(); 389 int tabWidth = Integer.parseInt(s); 390 if (tabWidth > 0 && tabWidth <= 8) 391 buffer.setTabWidth(tabWidth); 392 else 393 error = true; 394 } 395 catch (NumberFormatException e) { 396 error = true; 397 } 398 if (error) { 399 MessageDialog.showMessageDialog(editor, "Invalid tab width", "Error"); 400 return false; 401 } 402 } 403 if (indentSizeTextField != null) { 404 try { 405 String s = indentSizeTextField.getText(); 406 int indentSize = Integer.parseInt(s); 407 if (indentSize > 0 && indentSize <= 8) 408 buffer.setIndentSize(indentSize); 409 else 410 error = true; 411 } 412 catch (NumberFormatException e) { 413 error = true; 414 } 415 if (error) { 416 MessageDialog.showMessageDialog(editor, "Invalid indent size", "Error"); 417 return false; 418 } 419 } 420 if (wrapColumnTextField != null) { 421 try { 422 String s = wrapColumnTextField.getText(); 423 int wrapCol = Integer.parseInt(s); 424 if (wrapCol >= 16) 425 buffer.setProperty(Property.WRAP_COL, wrapCol); 426 else 427 error = true; 428 } 429 catch (NumberFormatException e) { 430 error = true; 431 } 432 if (error) { 433 MessageDialog.showMessageDialog(editor, "Invalid wrap column", "Error"); 434 return false; 435 } 436 } 437 if (useTabsCheckBox != null) 438 buffer.setProperty(Property.USE_TABS, useTabsCheckBox.isSelected()); 439 if (indentBeforeBraceCheckBox != null) 440 buffer.setProperty(Property.INDENT_BEFORE_BRACE, indentBeforeBraceCheckBox.isSelected()); 441 if (indentAfterBraceCheckBox != null) 442 buffer.setProperty(Property.INDENT_AFTER_BRACE, indentAfterBraceCheckBox.isSelected()); 443 if (lineSeparatorComboBox != null) { 444 String s = (String ) lineSeparatorComboBox.getSelectedItem(); 445 if (s.equals(TEXT_LF)) 446 buffer.lineSeparator = "\n"; 447 else if (s.equals(TEXT_CRLF)) 448 buffer.lineSeparator = "\r\n"; 449 else if (s.equals(TEXT_CR)) 450 buffer.lineSeparator = "\r"; 451 } 452 return true; 453 } 454 455 protected void ok() 456 { 457 if (save()) { 458 buffer.saveProperties(); 459 buffer.repaint(); 460 dispose(); 461 } 462 } 463 464 public static void properties() 465 { 466 final Editor editor = Editor.currentEditor(); 467 final Buffer buffer = editor.getBuffer(); 468 switch (buffer.getType()) { 469 case Buffer.TYPE_DIRECTORY: 470 case Buffer.TYPE_SHELL: 471 case Buffer.TYPE_TELNET: 472 case Buffer.TYPE_SSH: 473 case Buffer.TYPE_MAN: 474 return; 475 default: 476 break; 477 } 478 if (buffer.getModeId() == SEND_MAIL_MODE) 479 return; 480 if (buffer.getFile() == null) 481 return; 482 PropertiesDialog d = new PropertiesDialog(); 483 editor.centerDialog(d); 484 d.show(); 485 } 486 487 public static void listProperties() 488 { 489 final Editor editor = Editor.currentEditor(); 490 final Buffer buffer = editor.getBuffer(); 491 FastStringBuffer sb = new FastStringBuffer(); 492 PropertyList properties = buffer.getProperties(); 493 if (properties != null) { 494 Set keySet = properties.keySet(); 495 if (keySet != null) { 496 ArrayList keys = new ArrayList (keySet); 498 Collections.sort(keys); 499 Iterator it = keys.iterator(); 500 while (it.hasNext()) { 501 Property property = (Property) it.next(); 502 Object value = properties.getProperty(property); 503 sb.append(property.getDisplayName()); 504 sb.append(" = "); 505 sb.append(value); 506 sb.append('\n'); 507 } 508 } 509 } 510 if (sb.length() > 0) { 511 String output = sb.toString(); 512 OutputBuffer buf = OutputBuffer.getOutputBuffer(output); 513 buf.setFormatter(new PropertiesFormatter(buf)); 514 if (buf != null) { 515 sb.setText("listProperties"); 516 if (buffer.getFile() != null) { 517 sb.append(' '); 518 sb.append(buffer.getFile().getName()); 519 } 520 buf.setTitle(sb.toString()); 521 editor.makeNext(buf); 522 editor.displayInOtherWindow(buf); 523 } 524 } 525 } 526 } 527 | Popular Tags |