001: package org.enhydra.jawe.components.xpdlview;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.Dimension;
007: import java.awt.event.ActionEvent;
008: import java.awt.event.ActionListener;
009: import java.awt.event.MouseWheelEvent;
010: import java.awt.event.MouseWheelListener;
011: import java.io.ByteArrayOutputStream;
012:
013: import javax.swing.BorderFactory;
014: import javax.swing.Box;
015: import javax.swing.BoxLayout;
016: import javax.swing.JButton;
017: import javax.swing.JComboBox;
018: import javax.swing.JComponent;
019: import javax.swing.JLabel;
020: import javax.swing.JOptionPane;
021: import javax.swing.JPanel;
022: import javax.swing.SwingConstants;
023: import javax.swing.border.Border;
024: import javax.swing.text.PlainDocument;
025: import javax.xml.parsers.DocumentBuilder;
026: import javax.xml.parsers.DocumentBuilderFactory;
027: import javax.xml.transform.Transformer;
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.dom.DOMSource;
030: import javax.xml.transform.stream.StreamResult;
031:
032: import org.enhydra.jawe.JaWEComponent;
033: import org.enhydra.jawe.JaWEComponentView;
034: import org.enhydra.jawe.JaWEManager;
035: import org.enhydra.jawe.base.controller.JaWEFrame;
036: import org.enhydra.shark.xpdl.XMLAttribute;
037: import org.enhydra.shark.xpdl.XMLCollection;
038: import org.enhydra.shark.xpdl.XMLComplexChoice;
039: import org.enhydra.shark.xpdl.XMLComplexElement;
040: import org.enhydra.shark.xpdl.XMLElement;
041: import org.enhydra.shark.xpdl.XMLSimpleElement;
042: import org.enhydra.shark.xpdl.elements.Package;
043: import org.jedit.syntax.JEditTextArea;
044: import org.jedit.syntax.SyntaxDocument;
045: import org.jedit.syntax.SyntaxStyle;
046: import org.jedit.syntax.TextAreaPainter;
047: import org.jedit.syntax.Token;
048: import org.jedit.syntax.XMLTokenMarker;
049: import org.w3c.dom.Document;
050: import org.w3c.dom.Node;
051:
052: /**
053: * Container for displaying menubar, toolbar, process graphs ...
054: *
055: * @author Sasa Bojanic
056: * @author Miroslav Popov
057: * @author Zoran Milakovic
058: */
059: public class XPDLViewControllerPanel extends JPanel implements
060: JaWEComponentView {
061:
062: protected XPDLViewController controller;
063:
064: // protected JScrollPane xpdlPreviewSP;
065: protected JEditTextArea xpdlPreviewTA;
066:
067: protected JComboBox searchCB = new JComboBox();
068: protected Component searchPanel;
069:
070: protected XMLElement elementToShow;
071:
072: public XPDLViewControllerPanel(XPDLViewController controller) {
073: this .controller = controller;
074: }
075:
076: public void configure() {
077: }
078:
079: public void init() {
080: setBorder(BorderFactory.createEtchedBorder());
081: setLayout(new BorderLayout());
082:
083: add(createCenterComponent(), BorderLayout.CENTER);
084: searchPanel = createSearchPanel();
085: add(searchPanel, BorderLayout.NORTH);
086: }
087:
088: public JaWEComponent getJaWEComponent() {
089: return controller;
090: }
091:
092: public JComponent getDisplay() {
093: return this ;
094: }
095:
096: public void setSelectedElement(XMLElement pkgOrWpOrAs) {
097: this .elementToShow = pkgOrWpOrAs;
098: refreshView();
099: }
100:
101: public XMLElement getSelectedElement() {
102: return elementToShow;
103: }
104:
105: /**
106: * Create the center component of this panel.
107: */
108: protected Component createCenterComponent() {
109: // creating text area pane for xpdl preview
110: xpdlPreviewTA = new JEditTextArea();
111: // xpdlPreviewTA.setLineWrap(false);
112: // xpdlPreviewTA.setWrapStyleWord(false);
113: xpdlPreviewTA.setEditable(false);
114: xpdlPreviewTA.setMinimumSize(new Dimension(100, 100));
115: xpdlPreviewTA.setElectricScroll(3);
116: xpdlPreviewTA.addMouseWheelListener(new MouseWheelHandler(
117: xpdlPreviewTA));
118:
119: Color bkgCol = ((XPDLViewSettings) controller.getSettings())
120: .getBackgroundColor();
121: if (controller.getXPDLViewSettings().showHighlight()) {
122: SyntaxDocument doc = new SyntaxDocument();
123: doc.putProperty(PlainDocument.tabSizeAttribute,
124: new Integer(4));
125: xpdlPreviewTA.setDocument(doc);
126:
127: xpdlPreviewTA.setTokenMarker(new XMLTokenMarker());
128:
129: TextAreaPainter painter = xpdlPreviewTA.getPainter();
130: SyntaxStyle[] styles = painter.getStyles();
131: styles[Token.KEYWORD1] = new SyntaxStyle(new Color(0, 102,
132: 153), false, true);
133: styles[Token.KEYWORD2] = new SyntaxStyle(new Color(0, 153,
134: 102), false, true);
135: styles[Token.KEYWORD3] = new SyntaxStyle(new Color(0, 153,
136: 255), false, true);
137: styles[Token.LITERAL1] = new SyntaxStyle(new Color(255, 0,
138: 204), false, false);
139: styles[Token.LITERAL2] = new SyntaxStyle(new Color(204, 0,
140: 204), false, false);
141: painter.setStyles(styles);
142: painter.setEOLMarkersPainted(false);
143: painter.setBracketHighlightEnabled(true);
144: painter.setBackground(bkgCol);
145: }
146: xpdlPreviewTA.setBackground(bkgCol);
147: // xpdlPreviewSP=new JScrollPane();
148: // xpdlPreviewSP.setViewportView(xpdlPreviewTA);
149: // JViewport port = xpdlPreviewSP.getViewport();
150: // port.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
151: // return xpdlPreviewSP;
152: return xpdlPreviewTA;
153: }
154:
155: protected Component createSearchPanel() {
156: JPanel sp = new JPanel();
157: sp.setLayout(new BoxLayout(sp, BoxLayout.X_AXIS));
158:
159: Border emptyb = BorderFactory.createEmptyBorder(5, 5, 5, 5);
160: sp.setBorder(emptyb);
161:
162: JLabel jl = new JLabel(controller.getSettings()
163: .getLanguageDependentString("SearchForKey")
164: + ": ");
165: jl.setAlignmentX(Component.LEFT_ALIGNMENT);
166: jl.setAlignmentY(Component.BOTTOM_ALIGNMENT);
167: jl.setHorizontalAlignment(SwingConstants.RIGHT);
168:
169: searchCB.setEditable(true);
170: searchCB.setAlignmentX(Component.LEFT_ALIGNMENT);
171: searchCB.setAlignmentY(Component.BOTTOM_ALIGNMENT);
172: Dimension comboBoxDimension = new Dimension(200, 20);
173: searchCB.setMinimumSize(new Dimension(comboBoxDimension));
174: searchCB.setMaximumSize(new Dimension(comboBoxDimension));
175: searchCB.setPreferredSize(new Dimension(comboBoxDimension));
176: Color bkgCol = ((XPDLViewSettings) controller.getSettings())
177: .getBackgroundColor();
178: searchCB.setBackground(bkgCol);
179: searchCB.getEditor().getEditorComponent().setBackground(bkgCol);
180:
181: JButton jb = new JButton("");
182:
183: jb.setIcon(controller.getXPDLViewSettings().getFindNextIcon());
184:
185: jb.setToolTipText(controller.getSettings()
186: .getLanguageDependentString(
187: "FindNextStringOccuranceKey"));
188: jb.setVerticalTextPosition(SwingConstants.CENTER);
189: jb.setAlignmentX(Component.LEFT_ALIGNMENT);
190: jb.setAlignmentY(Component.BOTTOM_ALIGNMENT);
191: jb.setPreferredSize(new Dimension(20, 20));
192:
193: sp.add(jl);
194: sp.add(searchCB);
195: sp.add(Box.createHorizontalStrut(5));
196: sp.add(jb);
197:
198: jb.addActionListener(new ActionListener() {
199: public void actionPerformed(ActionEvent ae) {
200: findString();
201: }
202: });
203:
204: return sp;
205: }
206:
207: public void refreshView() {
208: try {
209: String toDisp = "";
210: xpdlPreviewTA.setText(toDisp);
211:
212: Document document = null;
213:
214: DocumentBuilderFactory dbf = DocumentBuilderFactory
215: .newInstance();
216: DocumentBuilder dbuilder = dbf.newDocumentBuilder();
217: document = dbuilder.newDocument();
218: ByteArrayOutputStream baos = new ByteArrayOutputStream();
219:
220: // Here we get all document elements
221: if (elementToShow != null) {
222:
223: if (!(elementToShow instanceof XMLAttribute)) {
224: if (elementToShow instanceof Package) {
225: JaWEManager.getInstance().getXPDLHandler()
226: .getXPDLRepositoryHandler().toXML(
227: document,
228: (Package) elementToShow);
229: } else {
230: Node node = document
231: .createElement(elementToShow.toName());
232: if (elementToShow instanceof XMLComplexElement) {
233: JaWEManager
234: .getInstance()
235: .getXPDLHandler()
236: .getXPDLRepositoryHandler()
237: .toXML(
238: node,
239: (XMLComplexElement) elementToShow);
240: } else if (elementToShow instanceof XMLCollection) {
241: JaWEManager
242: .getInstance()
243: .getXPDLHandler()
244: .getXPDLRepositoryHandler()
245: .toXML(
246: node,
247: (XMLCollection) elementToShow);
248: } else if (elementToShow instanceof XMLComplexChoice) {
249: JaWEManager
250: .getInstance()
251: .getXPDLHandler()
252: .getXPDLRepositoryHandler()
253: .toXML(
254: node,
255: (XMLComplexChoice) elementToShow);
256: } else if (elementToShow instanceof XMLSimpleElement) {
257: JaWEManager
258: .getInstance()
259: .getXPDLHandler()
260: .getXPDLRepositoryHandler()
261: .toXML(
262: node,
263: (XMLSimpleElement) elementToShow);
264: }
265: Node toAppend = node.getFirstChild();
266: if (toAppend == null) {
267: toAppend = node;
268: }
269: document.appendChild(toAppend);
270: }
271: // Use a Transformer for output
272: TransformerFactory tFactory = TransformerFactory
273: .newInstance();
274: Transformer transformer = tFactory.newTransformer();
275: transformer.setOutputProperty("indent", "yes");
276: transformer
277: .setOutputProperty(
278: "{http://xml.apache.org/xslt}indent-amount",
279: "4");
280: transformer.setOutputProperty("encoding",
281: JaWEManager.getInstance()
282: .getJaWEController()
283: .getControllerSettings()
284: .getEncoding());
285: if (!(elementToShow instanceof Package)) {
286: transformer.setOutputProperty(
287: "omit-xml-declaration", "yes");
288: }
289: DOMSource source = new DOMSource(document);
290: StreamResult result = new StreamResult(baos);
291: transformer.transform(source, result);
292:
293: toDisp = baos.toString(JaWEManager.getInstance()
294: .getJaWEController()
295: .getControllerSettings().getEncoding());
296: baos.close();
297:
298: } else {
299: toDisp = elementToShow.toName() + "=" + "\""
300: + elementToShow.toValue() + "\"";
301: }
302:
303: }
304:
305: xpdlPreviewTA.setText(toDisp);
306: xpdlPreviewTA.setCaretPosition(0);
307: } catch (Exception ex) {
308: ex.printStackTrace();
309: }
310: }
311:
312: /**
313: * Searches the XPDL context containd within text area to find wanted
314: * string. Search starts from the current cursor location. When some
315: * new text is entered into search box, it can be latter retrieved from
316: * the box.
317: */
318: protected void findString() {
319: if (searchCB.getSelectedItem() == null)
320: return;
321: String searchingTxt = searchCB.getSelectedItem().toString();
322: addItemToCombo(searchingTxt);
323: String txt = xpdlPreviewTA.getText();
324: try {
325: int foundPos = xpdlPreviewTA.getCaretPosition();
326: for (int i = foundPos; i <= (txt.length() - searchingTxt
327: .length()); i++) {
328: if (txt.substring(i, i + searchingTxt.length())
329: .equalsIgnoreCase(searchingTxt)) {
330: xpdlPreviewTA.setCaretPosition(i);
331: // xpdlPreviewTA.getCaret().setSelectionVisible(true);
332: xpdlPreviewTA.select(i, i + searchingTxt.length());
333: foundPos = i + searchingTxt.length();
334: return;
335: }
336: }
337: } catch (Exception ex) {
338: }
339: JaWEFrame jf = JaWEManager.getInstance().getJaWEController()
340: .getJaWEFrame();
341: JOptionPane.showMessageDialog(jf, controller.getSettings()
342: .getLanguageDependentString(
343: "MessageJaWEHasFinishedSearchingXPDL"), jf
344: .getAppTitle(), JOptionPane.INFORMATION_MESSAGE);
345: xpdlPreviewTA.setCaretPosition(0);
346: return;
347: }
348:
349: /**
350: * Adds a text into combo box if it hasn't been there previously.
351: */
352: private void addItemToCombo(String searchingTxt) {
353: if (searchingTxt == null || searchingTxt.trim().length() == 0)
354: return;
355: for (int i = 0; i < searchCB.getItemCount(); i++) {
356: if (searchCB.getItemAt(i).toString().equals(searchingTxt)) {
357: return;
358: }
359: }
360: searchCB.addItem(searchingTxt);
361: }
362:
363: public void changeLanguage() {
364: JLabel l = (JLabel) ((JPanel) searchPanel).getComponent(0);
365: JButton b = (JButton) ((JPanel) searchPanel).getComponent(3);
366: l.setText(controller.getSettings().getLanguageDependentString(
367: "SearchForKey")
368: + ": ");
369: b.setToolTipText(controller.getSettings()
370: .getLanguageDependentString(
371: "FindNextStringOccuranceKey"));
372: }
373:
374: }
375:
376: class MouseWheelHandler implements MouseWheelListener {
377: JEditTextArea area;
378:
379: public MouseWheelHandler(JEditTextArea area) {
380: this .area = area;
381: }
382:
383: public void mouseWheelMoved(MouseWheelEvent e) {
384: if (e.isControlDown()) {
385: if ((area.getFirstLine() + e.getWheelRotation()) < 0
386: || ((area.getFirstLine() + area.getVisibleLines() + e
387: .getWheelRotation()) > area.getLineCount()))
388: return;
389: area.setFirstLine(area.getFirstLine()
390: + e.getWheelRotation());
391: } else {
392: if ((area.getFirstLine() + e.getUnitsToScroll()) < 0
393: || ((area.getFirstLine() + area.getVisibleLines() + e
394: .getUnitsToScroll()) > area.getLineCount()))
395: return;
396: area.setFirstLine(area.getFirstLine()
397: + e.getUnitsToScroll());
398: }
399: }
400: }
|