KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > XPDLPreview


1 /* XPDLPreview.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11 package org.enhydra.jawe;
12
13 import java.awt.*;
14 import java.awt.event.*;
15 import java.io.*;
16
17 import javax.swing.*;
18 import javax.swing.border.*;
19
20 import javax.xml.parsers.*;
21 import javax.xml.transform.*;
22 import javax.xml.transform.dom.*;
23 import javax.xml.transform.stream.*;
24 import org.w3c.dom.*;
25
26 import org.enhydra.jawe.actions.*;
27
28 /**
29  * Shows the preview of XPDL that will be generated based on created graph.
30  */

31 public class XPDLPreview extends JPanel {
32
33    private AbstractEditor editor;
34
35    private JScrollPane xpdlPreviewSP;
36    private JTextArea xpdlPreviewTA;
37
38    private JComboBox searchCB=new JComboBox();
39    private Component searchPanel;
40
41    public XPDLPreview (AbstractEditor editor) {
42       super(true);
43       this.editor=editor;
44       setBorder(BorderFactory.createEtchedBorder());
45       setLayout(new BorderLayout());
46
47       add(createCenterComponent(),BorderLayout.CENTER);
48       searchPanel=createSearchPanel();
49       add(searchPanel,BorderLayout.NORTH);
50    }
51
52    /**
53     * Create the center component of this panel.
54     */

55    protected Component createCenterComponent() {
56       // creating text area pane for xpdl preview
57
xpdlPreviewTA=new JTextArea();
58       xpdlPreviewTA.setLineWrap(false);
59       xpdlPreviewTA.setWrapStyleWord(false);
60       xpdlPreviewTA.setEditable(false);
61
62       xpdlPreviewSP=new JScrollPane();
63       xpdlPreviewSP.setViewportView(xpdlPreviewTA);
64       JViewport port = xpdlPreviewSP.getViewport();
65       port.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
66       return xpdlPreviewSP;
67    }
68
69    protected Component createSearchPanel () {
70       JPanel sp=new JPanel();
71       sp.setLayout(new BoxLayout(sp,BoxLayout.X_AXIS));
72
73       Border emptyb=BorderFactory.createEmptyBorder(5,5,5,5);
74       sp.setBorder(emptyb);
75
76       JLabel jl=new JLabel(ResourceManager.getLanguageDependentString("SearchForKey")+": ");
77       jl.setAlignmentX(Component.LEFT_ALIGNMENT);
78       jl.setAlignmentY(Component.BOTTOM_ALIGNMENT);
79       jl.setHorizontalAlignment(SwingConstants.RIGHT);
80
81       searchCB.setEditable(true);
82       searchCB.setAlignmentX(Component.LEFT_ALIGNMENT);
83       searchCB.setAlignmentY(Component.BOTTOM_ALIGNMENT);
84       Dimension comboBoxDimension=new Dimension(200,20);
85       searchCB.setMinimumSize(new Dimension(comboBoxDimension));
86       searchCB.setMaximumSize(new Dimension(comboBoxDimension));
87       searchCB.setPreferredSize(new Dimension(comboBoxDimension));
88
89       JButton jb=new JButton("");
90       java.net.URL JavaDoc u = ResourceManager.getResource("FindNext"+JaWEConstants.IMAGE_SUFFIX);
91       if (u!=null) {
92          jb.setIcon(new ImageIcon(u));
93       }
94       jb.setToolTipText(ResourceManager.getLanguageDependentString("FindNextStringOccuranceKey"));
95       jb.setVerticalTextPosition(AbstractButton.CENTER);
96       jb.setAlignmentX(Component.LEFT_ALIGNMENT);
97       jb.setAlignmentY(Component.BOTTOM_ALIGNMENT);
98       jb.setPreferredSize(new Dimension(25,25));
99
100       sp.add(jl);
101       sp.add(searchCB);
102       sp.add(Box.createHorizontalStrut(5));
103       sp.add(jb);
104
105       jb.addActionListener(new ActionListener(){
106          public void actionPerformed( ActionEvent ae ){
107             findString();
108          }
109       });
110
111       return sp;
112    }
113
114    public void refreshView () {
115       try {
116          Document document = null;
117
118
119          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
120          DocumentBuilder dbuilder = dbf.newDocumentBuilder();
121          document = dbuilder.newDocument();
122          ByteArrayOutputStream baos=new ByteArrayOutputStream();
123
124          org.enhydra.jawe.xml.elements.Package pkg=
125                JaWE.getInstance().getPackageEditor().getXMLPackage();
126          // The extended attributes for all package elements must
127
// be updated if current package is not externally
128
// referenced package.
129
if (pkg==JaWE.getInstance().getRealXMLPackage()) {
130             Save.updateExtendedAttributesForWorkflowProcesses();
131          }
132
133          // Here we get all document elements
134
editor.getGraph().getXPDLObject().toXML(document);
135
136          // Use a Transformer for output
137
TransformerFactory tFactory =
138              TransformerFactory.newInstance();
139          Transformer transformer = tFactory.newTransformer();
140          transformer.setOutputProperty("indent","yes");
141          transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
142          transformer.setOutputProperty("encoding",JaWEConfig.getInstance().getEncoding());
143          if (editor instanceof ProcessEditor) {
144             transformer.setOutputProperty("omit-xml-declaration","yes");
145          }
146          DOMSource source = new DOMSource(document);
147          StreamResult result = new StreamResult(baos);
148          transformer.transform(source,result);
149
150          xpdlPreviewTA.setText(baos.toString(JaWEConfig.getInstance().getEncoding()));
151          xpdlPreviewTA.setCaretPosition(0);
152          baos.close();
153       } catch (Exception JavaDoc ex) {
154          ex.printStackTrace();
155          xpdlPreviewTA.setText("");
156       }
157    }
158
159    /**
160     * Searches the XPDL context containd within text area to find wanted
161     * string. Search starts from the current cursor location. When some
162     * new text is entered into search box, it can be latter retrieved from
163     * the box.
164     */

165    private void findString() {
166       if (searchCB.getSelectedItem()==null) return;
167       String JavaDoc searchingTxt=searchCB.getSelectedItem().toString();
168       addItemToCombo(searchingTxt);
169       String JavaDoc txt = xpdlPreviewTA.getText();
170       try {
171          int foundPos=xpdlPreviewTA.getCaretPosition();
172          for(int i=foundPos;i<=(txt.length() - searchingTxt.length());i++) {
173             if(txt.substring(i,i + searchingTxt.length()).equalsIgnoreCase(searchingTxt)) {
174                xpdlPreviewTA.setCaretPosition(i);
175                xpdlPreviewTA.getCaret().setSelectionVisible(true);
176                xpdlPreviewTA.select(i,i+searchingTxt.length());
177                foundPos = i+searchingTxt.length();
178                return;
179             }
180          }
181       } catch (Exception JavaDoc ex) {}
182       JOptionPane.showMessageDialog(editor.getWindow(),
183             ResourceManager.getLanguageDependentString("MessageJaWEHasFinishedSearchingXPDL"),
184             JaWE.getAppTitle(),JOptionPane.INFORMATION_MESSAGE);
185       xpdlPreviewTA.setCaretPosition(0);
186       return;
187    }
188
189    /**
190     * Adds a text into combo box if it hasn't been there previously.
191     */

192    private void addItemToCombo (String JavaDoc searchingTxt) {
193       if (searchingTxt==null || searchingTxt.trim().length()==0) return;
194       for (int i=0; i<searchCB.getItemCount(); i++) {
195          if (searchCB.getItemAt(i).toString().equals(searchingTxt)) {
196             return;
197          }
198       }
199       searchCB.addItem(searchingTxt);
200    }
201
202    public void changeLanguage () {
203       JLabel l=(JLabel)((JPanel)searchPanel).getComponent(0);
204       JButton b=(JButton)((JPanel)searchPanel).getComponent(3);
205       l.setText(ResourceManager.getLanguageDependentString("SearchForKey")+": ");
206       b.setToolTipText(ResourceManager.getLanguageDependentString("FindNextStringOccuranceKey"));
207    }
208
209 }
210
211
Popular Tags