001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: package com.ibm.richtext.demo;
014:
015: import java.awt.FileDialog;
016: import java.awt.Frame;
017: import java.awt.Toolkit;
018:
019: import java.io.File;
020:
021: import com.ibm.richtext.textlayout.attributes.AttributeMap;
022: import com.ibm.richtext.textlayout.attributes.TextAttribute;
023:
024: import com.ibm.richtext.textpanel.TextPanel;
025: import com.ibm.richtext.textpanel.TextPanelSettings;
026: import com.ibm.richtext.awtui.AwtMenuBuilder;
027:
028: public class CodeEdit extends EditApplication {
029:
030: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
031:
032: protected final TextPanelSettings fSettings;
033:
034: public static synchronized void main(String[] args) {
035:
036: if (args.length > 0 && args[0].equals("-swing")) {
037: new com.ibm.richtext.swingdemo.SwingCodeEdit(args, 1);
038: } else {
039: new CodeEdit(args, 0);
040: }
041: }
042:
043: protected CodeEdit(String[] args, int start) {
044:
045: super (Toolkit.getDefaultToolkit().getSystemClipboard(),
046: TextDocument.PLAIN_TEXT);
047:
048: AttributeMap defaultStyle = new AttributeMap(
049: TextAttribute.SIZE, new Float(12)).addAttribute(
050: TextAttribute.FAMILY, "Monospaced");
051:
052: fSettings = TextPanel.getDefaultSettings();
053: fSettings.setWraps(false);
054: fSettings.addDefaultValues(defaultStyle);
055:
056: if (args.length == start) {
057: doNewWindow();
058: } else {
059: boolean openedADocument = false;
060: for (int i = start; i < args.length; i++) {
061:
062: File file = new File(args[i]);
063: TextDocument document = null;
064: Throwable error = null;
065: try {
066: document = TextDocument.createFromFile(file,
067: TextDocument.PLAIN_TEXT);
068: } catch (Exception e) {
069: error = e;
070: }
071:
072: if (error != null) {
073: error.printStackTrace();
074: } else {
075: addDocument(document);
076: openedADocument = true;
077: }
078: }
079: if (!openedADocument) {
080: quit();
081: }
082: }
083: }
084:
085: protected DocumentWindow createDocumentWindow(TextDocument document) {
086:
087: return new AwtDocumentWindow(this , document, fSettings, false,
088: new SyntaxColorer(), false, true, menus);
089: }
090:
091: protected static final int[] menus = { AwtMenuBuilder.EDIT,
092: AwtMenuBuilder.BIDI, AwtMenuBuilder.ABOUT };
093:
094: public TextDocument openDocument(Frame dialogParent) {
095:
096: String title = ResourceUtils
097: .getString(EditorResources.OPEN_TITLE);
098:
099: File file = AwtDocumentWindow.getFileFromDialog(null, title,
100: dialogParent, FileDialog.LOAD);
101: if (file != null) {
102: try {
103: return TextDocument.createFromFile(file,
104: TextDocument.PLAIN_TEXT);
105: } catch (Exception e) {
106: System.out.print("");
107: }
108: }
109: return null;
110: }
111: }
|