Initial commit of newLISP.
[newlisp.git] / guiserver / java / ListBoxWidget.java
blob068fa921343df4e8bdcdd26da5542b24950c8ba8
1 //
2 // ListBoxWidget.java
3 // guiserver
4 //
5 // Created by Lutz Mueller on 5/15/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 java.awt.event.*;
27 import javax.swing.*;
28 import javax.swing.event.*;
29 import java.util.*;
32 @SuppressWarnings("unchecked")
33 public class ListBoxWidget extends gsObject {
35 JList list = null;
36 JScrollPane scrollpane;
37 DefaultListModel listModel;
39 public ListBoxWidget(StringTokenizer params)
41 scrollpane = new JScrollPane();
42 component = scrollpane;
43 container = scrollpane;
45 id = params.nextToken();
46 action = params.nextToken();
48 listModel = new DefaultListModel();
49 while(params.hasMoreTokens())
50 listModel.addElement(Base64Coder.decodeString(params.nextToken()));
52 list = new JList(listModel);
53 jcomponent = list;
54 scrollpane.getViewport().setView(list);
56 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
57 list.setSelectedIndex(0);
59 gsObject.widgets.put(id, this);
62 KeyListener keyListener = new KeyAdapter() {
63 public void keyTyped(KeyEvent e)
65 int index = list.getSelectedIndex();
66 String item = Base64Coder.encodeString((String)list.getSelectedValue());
67 guiserver.out.println("(" + action + " \"" + id + "\" " + index + " \"" + item + "\")");
68 guiserver.out.flush();
72 MouseListener mouseListener = new MouseAdapter() {
73 public void mouseClicked(MouseEvent e)
75 int clickCount = e.getClickCount();
76 int index = list.locationToIndex(e.getPoint());
78 if(action != null)
80 try {
81 String item = Base64Coder.encodeString((String)listModel.getElementAt(index));
82 guiserver.out.println("(" + action + " \"" + id + "\" " + index + " \"" + item + "\" " + clickCount + ")");
83 guiserver.out.flush();
84 } catch (Exception exc) { }
87 if(mouseEvent != null)
89 mouseEventOut(e, "clicked");
93 public void mousePressed(MouseEvent e)
95 if(mouseEvent != null) mouseEventOut(e, "pressed");
98 public void mouseReleased(MouseEvent e)
100 if(mouseEvent != null) mouseEventOut(e, "released");
105 list.addMouseListener(mouseListener);
106 list.addKeyListener(keyListener);
109 public void addListItem(StringTokenizer tokens)
111 while(tokens.hasMoreTokens())
113 String item = Base64Coder.decodeString(tokens.nextToken());
114 listModel.addElement(item);
118 public void removeListItem(StringTokenizer tokens)
120 int index = 0;
122 while(tokens.hasMoreTokens())
124 index = Integer.parseInt(tokens.nextToken());
125 if(index > (listModel.size() - 1)) index = listModel.size() - 1;
126 if(index < 0) index = 0;
127 listModel.remove(index);
131 public void clearList(StringTokenizer tokens)
133 while(listModel.size() > 0)
134 listModel.remove(0);
137 public void insertListItem(StringTokenizer tokens)
139 int index = 0;
140 String text;
142 while(tokens.hasMoreTokens())
144 text = Base64Coder.decodeString(tokens.nextToken());
145 index = Integer.parseInt(tokens.nextToken());
146 if(index > (listModel.size() - 1)) index = listModel.size() - 1;
147 if(index < 0) index = 0;
148 listModel.insertElementAt(text, index);
153 public void selectListItem(StringTokenizer tokens)
155 String item = Base64Coder.decodeString(tokens.nextToken());
156 Boolean flag = false;
158 if(tokens.hasMoreTokens())
159 flag = tokens.nextToken().equals("true");
161 list.setSelectedValue(item, flag);
165 public void scrollDown()
167 JScrollBar vbar = scrollpane.getVerticalScrollBar();
168 vbar.setValue(vbar.getMinimum());
171 public void registerMouseEvent(StringTokenizer tokens)
173 mouseEvent = tokens.nextToken();
176 private void mouseEventOut(MouseEvent e, String type)
178 guiserver.out.println("(" + mouseEvent + " \"" + id + "\" \"" + type + "\" " + e.getX() + " " + e.getY() + " " +
179 e.getButton() + " " + e.getClickCount() + " " + e.getModifiers() + ")");
180 guiserver.out.flush();
186 // eof //