Initial commit of newLISP.
[newlisp.git] / guiserver / java / TabbedPaneWidget.java
blob099f1c2d3aa767d4230fa1814481afb90426dec7
1 //
2 // TabbedPaneWidget.java
3 // guiserver
4 //
5 // Created by Lutz Mueller on 5/17/07.
6 //
7 //
8 // Copyright (C) 2007 Lutz Mueller
9 //
10 // This program is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 import java.awt.*;
26 import javax.swing.*;
27 import javax.swing.event.*;
28 import java.util.*;
30 @SuppressWarnings("unchecked")
31 public class TabbedPaneWidget extends gsObject {
33 JTabbedPane tabbedpane;
34 HashMap tabsHash;
36 public TabbedPaneWidget(StringTokenizer params)
38 id = params.nextToken();
39 action = params.nextToken();
41 String orientation = params.nextToken();
42 if(orientation.equals("bottom")) tabbedpane = new JTabbedPane(JTabbedPane.BOTTOM);
43 else if(orientation.equals("left")) tabbedpane = new JTabbedPane(JTabbedPane.LEFT);
44 else if (orientation.equals("right")) tabbedpane = new JTabbedPane(JTabbedPane.RIGHT);
45 else tabbedpane = new JTabbedPane(JTabbedPane.TOP);
47 component = tabbedpane;
48 jcomponent = tabbedpane;
49 container = tabbedpane;
50 tabsHash = new HashMap();
52 while(params.hasMoreTokens())
54 String cid = params.nextToken();
55 gsObject gsobject = (gsObject)gsObject.widgets.get(cid);
56 String title = Base64Coder.decodeString(params.nextToken());
57 tabbedpane.addTab(title, gsobject.component);
58 tabsHash.put(gsobject.component, cid);
61 gsObject.widgets.put(id, this);
62 //guiserver.widgets.put(id, tabbedpane);
64 ChangeListener changelistener = new ChangeListener() {
65 public void stateChanged(ChangeEvent e)
67 int index = tabbedpane.getSelectedIndex();
68 String title = Base64Coder.encodeString(tabbedpane.getTitleAt(index));
69 //Component cmpnt = tabbedpane.getComponentAt(index);
70 //Object obj = cmpnt;
71 //gsObject gso = (gsObject)obj;
73 String cid = (String)tabsHash.get(tabbedpane.getComponentAt(index));
75 guiserver.out.println("("+ action + " \"" + id + "\" \"" + cid + "\" \"" + title + "\" " + index + ")");
76 guiserver.out.flush();
80 tabbedpane.addChangeListener(changelistener);
84 public void insertTab(StringTokenizer tokens)
86 String scomp = tokens.nextToken();
87 String text = Base64Coder.decodeString(tokens.nextToken());
88 int index = 0;
89 ImageIcon icon = null;
91 if(tokens.hasMoreTokens())
92 index = Integer.parseInt(tokens.nextToken());
94 if(tokens.hasMoreTokens())
95 icon = guiserver.getIconFromPath(Base64Coder.decodeString(tokens.nextToken()), this.getClass());
97 if(index > tabbedpane.getTabCount())
98 index = tabbedpane.getTabCount();
100 gsObject gsobject = (gsObject)gsObject.widgets.get(scomp);
101 Component comp = gsobject.component;
103 tabbedpane.insertTab(text, icon, comp, null, index);
104 tabsHash.put(comp, gsobject.id);
107 public void removeTab(StringTokenizer tokens)
109 int index = 0;
110 int tcount = tabbedpane.getTabCount();
112 if(tcount == 1) return;
114 if(tokens.hasMoreTokens())
115 index = Integer.parseInt(tokens.nextToken());
118 if(index >= tcount)
119 index = tcount - 1;
120 if(index < 0) index = 0;
122 Component comp = tabbedpane.getComponentAt(index);
123 tabsHash.remove(comp);
125 try { tabbedpane.removeTabAt(index); } catch (IndexOutOfBoundsException ex) {}
129 public void setIcon(StringTokenizer tokens)
131 ImageIcon icon = guiserver.getIconFromPath(Base64Coder.decodeString(tokens.nextToken()), this.getClass());
133 int index = Integer.parseInt(tokens.nextToken());
135 if(index >= tabbedpane.getTabCount())
136 index = tabbedpane.getTabCount() - 1;
137 if(index < 0) index = 0;
139 tabbedpane.setIconAt(index, icon);
142 public void setText(StringTokenizer tokens)
144 String title = Base64Coder.decodeString(tokens.nextToken());
145 int index = Integer.parseInt(tokens.nextToken());
147 if(index >= tabbedpane.getTabCount())
148 index = tabbedpane.getTabCount() - 1;
149 if(index < 0) index = 0;
151 tabbedpane.setTitleAt(index, title);
155 public void requestFocus(StringTokenizer tokens)
157 int index = Integer.parseInt(tokens.nextToken());
159 if(index >= tabbedpane.getTabCount())
160 index = tabbedpane.getTabCount() - 1;
161 if(index < 0) index = 0;
163 tabbedpane.setSelectedIndex(index);
169 // eof //