001: /*
002: * PropertiesDialog.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: PropertiesDialog.java,v 1.7 2003/08/04 16:15:01 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Cursor;
025: import java.awt.Dimension;
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.Date;
029: import java.util.Iterator;
030: import java.util.Set;
031: import java.util.Vector;
032: import javax.swing.BorderFactory;
033: import javax.swing.Box;
034: import javax.swing.BoxLayout;
035: import javax.swing.JComboBox;
036: import javax.swing.JPanel;
037: import javax.swing.JTextField;
038: import javax.swing.border.Border;
039:
040: public final class PropertiesDialog extends AbstractDialog implements
041: Constants {
042: private static final String TEXT_LF = "LF";
043: private static final String TEXT_CR = "CR";
044: private static final String TEXT_CRLF = "CR+LF";
045:
046: private final Editor editor;
047: private final Buffer buffer;
048:
049: private JComboBox modeComboBox;
050: private JTextField tabWidthTextField;
051: private JTextField indentSizeTextField;
052: private JTextField wrapColumnTextField;
053: private CheckBox useTabsCheckBox;
054: private CheckBox indentBeforeBraceCheckBox;
055: private CheckBox indentAfterBraceCheckBox;
056: private JComboBox lineSeparatorComboBox;
057:
058: public PropertiesDialog() {
059: super (Editor.currentEditor(), "Properties", true);
060:
061: editor = Editor.currentEditor();
062: buffer = editor.getBuffer();
063: final int modeId = buffer.getModeId();
064:
065: final int WIDTH = 30;
066:
067: if (!buffer.isUntitled()) {
068: JPanel group = new JPanel();
069: group.setLayout(new BoxLayout(group, BoxLayout.Y_AXIS));
070: group.setAlignmentX(LEFT_ALIGNMENT);
071: Border etchedBorder = BorderFactory.createEtchedBorder();
072: Border titledBorder = BorderFactory.createTitledBorder(
073: etchedBorder, "File");
074: group.setBorder(titledBorder);
075:
076: final File file = buffer.getFile();
077: StaticTextField fileTextField = new StaticTextField(file
078: .isRemote() ? file.netPath() : file.canonicalPath());
079: group.add(fileTextField);
080:
081: StaticTextField modificationTextField = null;
082:
083: if (file.getProtocol() == File.PROTOCOL_HTTP) {
084: if (buffer.getLastModified() != 0) {
085: Date date = new Date(buffer.getLastModified());
086: modificationTextField = new StaticTextField(
087: "Last modified ".concat(date.toString()));
088: }
089: } else if (file.isLocal()) {
090: Date date = new Date(file.lastModified());
091: FastStringBuffer sb = new FastStringBuffer();
092: if (buffer.isModified())
093: sb.append("Modified:");
094: else
095: sb.append("Not modified;");
096: sb.append(" last saved ");
097: sb.append(date.toString());
098: modificationTextField = new StaticTextField(sb
099: .toString());
100: }
101:
102: if (modificationTextField != null) {
103: group.add(Box.createVerticalStrut(6));
104: group.add(modificationTextField);
105: }
106:
107: String encoding = buffer.getSaveEncoding();
108: if (encoding != null) {
109: group.add(Box.createVerticalStrut(6));
110: group.add(new StaticTextField(encoding));
111: } else
112: Debug.bug();
113:
114: buffer.checkCVS();
115: CVSEntry cvsEntry = buffer.getCVSEntry();
116: if (cvsEntry != null) {
117: String revision = cvsEntry.getRevision();
118: if (revision != null && revision.length() > 0) {
119: FastStringBuffer sb = new FastStringBuffer("CVS ");
120: if (revision.equals("0")) {
121: sb.append(" (locally added)");
122: } else {
123: sb.append(" revision ");
124: sb.append(revision);
125: if (buffer.getLastModified() != cvsEntry
126: .getCheckoutTime())
127: sb.append(" (locally modified)");
128: }
129: group.add(Box.createVerticalStrut(6));
130: group.add(new StaticTextField(sb.toString()));
131: }
132: }
133:
134: if (Editor.checkExperimental()) {
135: String s = P4.getStatusString(file);
136: if (s != null) {
137: group.add(Box.createVerticalStrut(6));
138: group.add(new StaticTextField(s));
139: }
140: }
141:
142: if (buffer.isReadOnly()) {
143: if (file.getProtocol() == File.PROTOCOL_HTTP)
144: ;
145: else if (modeId == BINARY_MODE)
146: ;
147: else if (modeId == IMAGE_MODE)
148: ;
149: else if (modeId == WEB_MODE)
150: ;
151: else {
152: StaticTextField readOnlyTextField = new StaticTextField(
153: "File is read only on disk", WIDTH);
154: group.add(Box.createVerticalStrut(6));
155: group.add(readOnlyTextField);
156: }
157: }
158:
159: mainPanel.add(group);
160: }
161:
162: if (modeId != IMAGE_MODE && modeId != WEB_MODE) {
163: // Mode combo box.
164: modeComboBox = new JComboBox(getPermissibleModes());
165: Dimension dim = modeComboBox.getPreferredSize();
166: modeComboBox.setMinimumSize(dim);
167: modeComboBox.setMaximumSize(dim);
168:
169: JPanel flow = new JPanel();
170: flow.setLayout(new BoxLayout(flow, BoxLayout.X_AXIS));
171: flow.setAlignmentX(LEFT_ALIGNMENT);
172: Label label = new Label("Mode:");
173: flow.add(label);
174: label.setDisplayedMnemonic('M');
175:
176: modeComboBox.setSelectedItem(buffer.getMode().toString());
177: label.setLabelFor(modeComboBox);
178: modeComboBox.addKeyListener(this );
179:
180: flow.add(Box.createHorizontalStrut(5));
181: flow.add(modeComboBox);
182:
183: addVerticalStrut();
184: mainPanel.add(flow);
185:
186: if (modeId != ARCHIVE_MODE && modeId != BINARY_MODE) {
187: flow = new JPanel();
188: flow.setLayout(new BoxLayout(flow, BoxLayout.X_AXIS));
189: flow.setAlignmentX(LEFT_ALIGNMENT);
190: label = new Label("Tab width:");
191: label.setDisplayedMnemonic('T');
192: flow.add(label);
193: flow.add(Box.createHorizontalStrut(5));
194: tabWidthTextField = new JTextField(3);
195: dim = tabWidthTextField.getPreferredSize();
196: tabWidthTextField.setMinimumSize(dim);
197: tabWidthTextField.setMaximumSize(dim);
198: tabWidthTextField.setAlignmentX(LEFT_ALIGNMENT);
199: tabWidthTextField.setText(String.valueOf(buffer
200: .getTabWidth()));
201: label.setLabelFor(tabWidthTextField);
202: flow.add(tabWidthTextField);
203:
204: flow.add(Box.createHorizontalStrut(5));
205:
206: label = new Label("Indent size:");
207: label.setDisplayedMnemonic('I');
208: flow.add(label);
209: flow.add(Box.createHorizontalStrut(5));
210: indentSizeTextField = new JTextField(3);
211: dim = indentSizeTextField.getPreferredSize();
212: indentSizeTextField.setMinimumSize(dim);
213: indentSizeTextField.setMaximumSize(dim);
214: indentSizeTextField.setAlignmentX(LEFT_ALIGNMENT);
215: indentSizeTextField.setText(String.valueOf(buffer
216: .getIndentSize()));
217: label.setLabelFor(indentSizeTextField);
218: flow.add(indentSizeTextField);
219:
220: flow.add(Box.createHorizontalStrut(5));
221:
222: label = new Label("Wrap column:");
223: label.setDisplayedMnemonic('W');
224: flow.add(label);
225: flow.add(Box.createHorizontalStrut(5));
226: wrapColumnTextField = new JTextField(3);
227: dim = wrapColumnTextField.getPreferredSize();
228: wrapColumnTextField.setMinimumSize(dim);
229: wrapColumnTextField.setMaximumSize(dim);
230: wrapColumnTextField.setAlignmentX(LEFT_ALIGNMENT);
231: wrapColumnTextField.setText(String.valueOf(buffer
232: .getIntegerProperty(Property.WRAP_COL)));
233: label.setLabelFor(wrapColumnTextField);
234: flow.add(wrapColumnTextField);
235:
236: addVerticalStrut();
237: mainPanel.add(flow);
238:
239: useTabsCheckBox = new CheckBox("Use tabs", buffer
240: .getUseTabs());
241: useTabsCheckBox.setMnemonic('U');
242: addVerticalStrut();
243: addCheckBox(useTabsCheckBox);
244: useTabsCheckBox.setEnabled(true);
245:
246: switch (modeId) {
247: case JAVA_MODE:
248: case JAVASCRIPT_MODE:
249: case C_MODE:
250: case CPP_MODE:
251: case PERL_MODE:
252: case TCL_MODE:
253: indentBeforeBraceCheckBox = new CheckBox(
254: "Indent before '{'",
255: buffer
256: .getBooleanProperty(Property.INDENT_BEFORE_BRACE));
257: indentBeforeBraceCheckBox.setMnemonic('b');
258: addVerticalStrut();
259: addCheckBox(indentBeforeBraceCheckBox);
260: indentAfterBraceCheckBox = new CheckBox(
261: "Indent after '{'",
262: buffer
263: .getBooleanProperty(Property.INDENT_AFTER_BRACE));
264: indentAfterBraceCheckBox.setMnemonic('a');
265: addVerticalStrut();
266: addCheckBox(indentAfterBraceCheckBox);
267: default:
268: break;
269: }
270:
271: addVerticalStrut();
272:
273: // Line separator combo box.
274: Vector v = new Vector();
275: v.add(TEXT_LF);
276: v.add(TEXT_CRLF);
277: v.add(TEXT_CR);
278: lineSeparatorComboBox = new JComboBox(v);
279: dim = lineSeparatorComboBox.getPreferredSize();
280: lineSeparatorComboBox.setMinimumSize(dim);
281: lineSeparatorComboBox.setMaximumSize(dim);
282:
283: flow = new JPanel();
284: flow.setLayout(new BoxLayout(flow, BoxLayout.X_AXIS));
285: flow.setAlignmentX(LEFT_ALIGNMENT);
286: label = new Label("Use");
287: flow.add(label);
288: label.setDisplayedMnemonic('L');
289:
290: if (buffer.lineSeparator == null)
291: buffer.lineSeparator = System
292: .getProperty("line.separator");
293:
294: if (buffer.lineSeparator.equals("\n"))
295: lineSeparatorComboBox.setSelectedItem(TEXT_LF);
296: else if (buffer.lineSeparator.equals("\r\n"))
297: lineSeparatorComboBox.setSelectedItem(TEXT_CRLF);
298: else if (buffer.lineSeparator.equals("\r"))
299: lineSeparatorComboBox.setSelectedItem(TEXT_CR);
300:
301: label.setLabelFor(lineSeparatorComboBox);
302:
303: flow.add(Box.createHorizontalStrut(5));
304: flow.add(lineSeparatorComboBox);
305:
306: flow.add(Box.createHorizontalStrut(5));
307: flow
308: .add(new Label(
309: "as line separator when saving file"));
310:
311: mainPanel.add(flow);
312: }
313:
314: addVerticalStrut();
315: }
316:
317: addOKCancel();
318:
319: if (tabWidthTextField != null)
320: tabWidthTextField.addKeyListener(this );
321:
322: if (indentSizeTextField != null)
323: indentSizeTextField.addKeyListener(this );
324:
325: if (wrapColumnTextField != null)
326: wrapColumnTextField.addKeyListener(this );
327:
328: pack();
329:
330: if (tabWidthTextField != null)
331: tabWidthTextField.requestFocus();
332: }
333:
334: private String[] getPermissibleModes() {
335: ModeList modeList = Editor.getModeList();
336: int fileType = buffer.getFileType();
337: if (fileType == FILETYPE_ZIP) {
338: String[] array = new String[2];
339: array[0] = ARCHIVE_MODE_NAME;
340: array[1] = BINARY_MODE_NAME;
341: return array;
342: }
343: if (fileType == FILETYPE_BINARY || fileType == FILETYPE_JPEG) {
344: String[] array;
345: if (Editor.getModeList().modeAccepts(IMAGE_MODE,
346: buffer.getFile().getName())) {
347: array = new String[2];
348: array[0] = IMAGE_MODE_NAME;
349: array[1] = BINARY_MODE_NAME;
350: } else {
351: array = new String[1];
352: array[0] = BINARY_MODE_NAME;
353: }
354: return array;
355: }
356: ArrayList list = new ArrayList();
357: synchronized (modeList) {
358: Iterator it = modeList.iterator();
359: while (it.hasNext()) {
360: ModeListEntry entry = (ModeListEntry) it.next();
361: if (entry.isSelectable())
362: list.add(entry.getDisplayName());
363: }
364: }
365: return (String[]) list.toArray(new String[list.size()]);
366: }
367:
368: private boolean save() {
369: if (modeComboBox != null) {
370: String modeName = (String) modeComboBox.getSelectedItem();
371: if (modeName != null
372: && !modeName.equals(buffer.getModeName())) {
373: Mode newMode = Editor.getModeList()
374: .getModeFromModeName(modeName);
375: if (buffer.getModeId() == BINARY_MODE
376: || newMode.getId() == BINARY_MODE) {
377: if (buffer.isModified()) {
378: FastStringBuffer sb = new FastStringBuffer(
379: "Buffer will be reloaded in ");
380: if (newMode.getId() == BINARY_MODE)
381: sb.append("binary");
382: else
383: sb.append(newMode.toString());
384: sb.append(" mode; discard changes?");
385: boolean confirmed = editor.confirm(buffer
386: .getFile().getName(), sb.toString());
387: if (!confirmed)
388: return false;
389: }
390: }
391: paint(getGraphics());
392: editor.repaintNow();
393: setCursor(Cursor
394: .getPredefinedCursor(Cursor.WAIT_CURSOR));
395: editor.setWaitCursor();
396: buffer.changeMode(newMode);
397: editor.setDefaultCursor();
398: setCursor(Cursor.getDefaultCursor());
399: }
400: }
401: boolean error = false;
402: if (tabWidthTextField != null) {
403: try {
404: String s = tabWidthTextField.getText();
405: int tabWidth = Integer.parseInt(s);
406: if (tabWidth > 0 && tabWidth <= 8)
407: buffer.setTabWidth(tabWidth);
408: else
409: error = true;
410: } catch (NumberFormatException e) {
411: error = true;
412: }
413: if (error) {
414: MessageDialog.showMessageDialog(editor,
415: "Invalid tab width", "Error");
416: return false;
417: }
418: }
419: if (indentSizeTextField != null) {
420: try {
421: String s = indentSizeTextField.getText();
422: int indentSize = Integer.parseInt(s);
423: if (indentSize > 0 && indentSize <= 8)
424: buffer.setIndentSize(indentSize);
425: else
426: error = true;
427: } catch (NumberFormatException e) {
428: error = true;
429: }
430: if (error) {
431: MessageDialog.showMessageDialog(editor,
432: "Invalid indent size", "Error");
433: return false;
434: }
435: }
436: if (wrapColumnTextField != null) {
437: try {
438: String s = wrapColumnTextField.getText();
439: int wrapCol = Integer.parseInt(s);
440: if (wrapCol >= 16)
441: buffer.setProperty(Property.WRAP_COL, wrapCol);
442: else
443: error = true;
444: } catch (NumberFormatException e) {
445: error = true;
446: }
447: if (error) {
448: MessageDialog.showMessageDialog(editor,
449: "Invalid wrap column", "Error");
450: return false;
451: }
452: }
453: if (useTabsCheckBox != null)
454: buffer.setProperty(Property.USE_TABS, useTabsCheckBox
455: .isSelected());
456: if (indentBeforeBraceCheckBox != null)
457: buffer.setProperty(Property.INDENT_BEFORE_BRACE,
458: indentBeforeBraceCheckBox.isSelected());
459: if (indentAfterBraceCheckBox != null)
460: buffer.setProperty(Property.INDENT_AFTER_BRACE,
461: indentAfterBraceCheckBox.isSelected());
462: if (lineSeparatorComboBox != null) {
463: String s = (String) lineSeparatorComboBox.getSelectedItem();
464: if (s.equals(TEXT_LF))
465: buffer.lineSeparator = "\n";
466: else if (s.equals(TEXT_CRLF))
467: buffer.lineSeparator = "\r\n";
468: else if (s.equals(TEXT_CR))
469: buffer.lineSeparator = "\r";
470: }
471: return true;
472: }
473:
474: protected void ok() {
475: if (save()) {
476: buffer.saveProperties();
477: buffer.repaint();
478: dispose();
479: }
480: }
481:
482: public static void properties() {
483: final Editor editor = Editor.currentEditor();
484: final Buffer buffer = editor.getBuffer();
485: switch (buffer.getType()) {
486: case Buffer.TYPE_DIRECTORY:
487: case Buffer.TYPE_SHELL:
488: case Buffer.TYPE_TELNET:
489: case Buffer.TYPE_SSH:
490: case Buffer.TYPE_MAN:
491: return;
492: default:
493: break;
494: }
495: if (buffer.getModeId() == SEND_MAIL_MODE)
496: return;
497: if (buffer.getFile() == null)
498: return;
499: PropertiesDialog d = new PropertiesDialog();
500: editor.centerDialog(d);
501: d.show();
502: }
503:
504: public static void listProperties() {
505: final Editor editor = Editor.currentEditor();
506: final Buffer buffer = editor.getBuffer();
507: FastStringBuffer sb = new FastStringBuffer();
508: PropertyList properties = buffer.getProperties();
509: if (properties != null) {
510: Set keySet = properties.keySet();
511: if (keySet != null) {
512: // Sort keys.
513: ArrayList keys = new ArrayList(keySet);
514: Collections.sort(keys);
515: Iterator it = keys.iterator();
516: while (it.hasNext()) {
517: Property property = (Property) it.next();
518: Object value = properties.getProperty(property);
519: sb.append(property.getDisplayName());
520: sb.append(" = ");
521: sb.append(value);
522: sb.append('\n');
523: }
524: }
525: }
526: if (sb.length() > 0) {
527: String output = sb.toString();
528: OutputBuffer buf = OutputBuffer.getOutputBuffer(output);
529: buf.setFormatter(new PropertiesFormatter(buf));
530: if (buf != null) {
531: sb.setText("listProperties");
532: if (buffer.getFile() != null) {
533: sb.append(' ');
534: sb.append(buffer.getFile().getName());
535: }
536: buf.setTitle(sb.toString());
537: editor.makeNext(buf);
538: editor.displayInOtherWindow(buf);
539: }
540: }
541: }
542: }
|