KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > wfxml > WfXMLComboPanel


1 /* WfXMLComboPanel.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.wfxml;
12
13 import java.io.*;
14 import java.awt.Component JavaDoc;
15 import java.awt.Dimension JavaDoc;
16 import javax.swing.*;
17 import javax.swing.JLabel JavaDoc;
18 import org.enhydra.jawe.*;
19 import org.enhydra.jawe.xml.XMLButton;
20 import org.enhydra.jawe.xml.XMLElement;
21 import org.enhydra.jawe.xml.XMLUtil;
22 import org.enhydra.jawe.xml.panels.XMLComboPanel;
23 import org.enhydra.jawe.xml.panels.XMLPanel;
24
25 import java.util.*;
26 import org.enhydra.jawe.xml.*;
27
28 import java.awt.Dimension JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Vector JavaDoc;
34 import javax.swing.Box JavaDoc;
35 import javax.swing.JComboBox JavaDoc;
36 import javax.swing.JOptionPane JavaDoc;
37 import javax.swing.JTable JavaDoc;
38 import javax.swing.table.DefaultTableModel JavaDoc;
39 import org.enhydra.jawe.JaWE;
40 import org.enhydra.jawe.xml.*;
41 import org.enhydra.jawe.xml.panels.*;
42 import org.w3c.dom.Node JavaDoc;
43
44 /**
45  * Creates panel with JLabel and JComboBox.
46  */

47 public class WfXMLComboPanel extends XMLComboPanel {
48
49    private static final int MAX_URL_HISTORY_SIZE=16;
50
51    private static String JavaDoc URL_HISTORY_FILE_PATH;
52    private static ArrayList urls;
53
54    private Dimension JavaDoc comboBoxDimension=new Dimension JavaDoc(350,20);
55
56    private XMLButton buttonConnect;
57
58
59    public WfXMLComboPanel (XMLElement myOwner) {
60
61       super(myOwner,null,XMLPanel.BOX_LAYOUT,false,true);
62
63       ((JLabel JavaDoc)getComponent(1)).setText(ResourceManager.getLanguageDependentString("RegistryServiceURL")+": ");
64
65       if (URL_HISTORY_FILE_PATH==null) {
66          URL_HISTORY_FILE_PATH=JaWEConstants.JAWE_USER_HOME+JaWEConstants.RURL_FILENAME;
67          File extR=new File(URL_HISTORY_FILE_PATH);
68          if (extR.exists()) {
69             urls=(ArrayList)readFile(URL_HISTORY_FILE_PATH);
70          }
71          if (urls==null) {
72             urls=new ArrayList();
73          }
74       }
75
76       final JComboBox JavaDoc jcb=super.getComboBox();
77       for (int i=0; i<urls.size(); i++) {
78          jcb.addItem(urls.get(i));
79       }
80       if (urls.size()>0) {
81          jcb.setSelectedIndex(0);
82       }
83       jcb.setPreferredSize(comboBoxDimension);
84       String JavaDoc s=XMLUtil.getLanguageDependentString("ConnectKey");
85       double w=0;
86       try {
87          w=getFontMetrics(getFont()).stringWidth(s);
88       } catch(Exception JavaDoc ex) {}
89       double h=getFontMetrics(getFont()).getHeight();
90       w+=25;
91       if (w<30) w=30;
92       Dimension JavaDoc dim=new Dimension JavaDoc((int)w,(int)h);
93
94       buttonConnect=new XMLButton(s,"Connect",dim);
95       buttonConnect.setAlignmentX(Component.LEFT_ALIGNMENT);
96       buttonConnect.setAlignmentY(Component.BOTTOM_ALIGNMENT);
97
98       add(Box.createRigidArea(new Dimension JavaDoc(10,0)));
99       add(buttonConnect);
100
101       buttonConnect.addActionListener(new ActionListener JavaDoc(){
102                public void actionPerformed(ActionEvent JavaDoc ae){
103                   String JavaDoc selected=(String JavaDoc)jcb.getSelectedItem();
104                   if (selected!=null) {
105                      addItemToCombo(selected);
106                   }
107                }
108             });
109
110    }
111
112    public XMLButton getConnectButton () {
113       return buttonConnect;
114    }
115
116    public void setElements () {
117    }
118
119    /**
120     * Adds a text into combo box if it hasn't been there previously.
121     */

122    private void addItemToCombo (String JavaDoc url) {
123       if (url==null || url.trim().length()==0) return;
124       JComboBox JavaDoc jcb=getComboBox();
125       for (int i=0; i<jcb.getItemCount(); i++) {
126          if (jcb.getItemAt(i).toString().equals(url)) {
127             jcb.removeItemAt(i);
128             jcb.insertItemAt(url,0);
129             jcb.setSelectedIndex(0);
130             urls.remove(i);
131             urls.add(0,url);
132             writeFile(urls,URL_HISTORY_FILE_PATH);
133             return;
134          }
135       }
136       jcb.insertItemAt(url,0);
137       urls.add(0,url);
138       if (jcb.getItemCount()>MAX_URL_HISTORY_SIZE) {
139          jcb.removeItemAt(MAX_URL_HISTORY_SIZE);
140          urls.remove(MAX_URL_HISTORY_SIZE);
141       }
142       jcb.setSelectedIndex(0);
143       writeFile(urls,URL_HISTORY_FILE_PATH);
144    }
145
146    private void writeFile (Object JavaDoc obj,String JavaDoc fName) {
147       try {
148          OutputStream fos = new FileOutputStream(fName);
149          ObjectOutputStream oout = new ObjectOutputStream(fos);
150          oout.writeObject(obj);
151          oout.flush();
152          oout.close();
153          fos.close();
154       } catch (Exception JavaDoc ex) {
155          ex.printStackTrace();
156       }
157    }
158
159    private Object JavaDoc readFile (String JavaDoc fName) {
160       try {
161          InputStream fis = new FileInputStream(fName);
162          ObjectInputStream oin = new ObjectInputStream(fis);
163          Object JavaDoc obj=oin.readObject();
164          oin.close();
165          return obj;
166       } catch (Exception JavaDoc ex) {
167          ex.printStackTrace();
168          return null;
169       }
170    }
171
172
173 }
174
175
Popular Tags