OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / java / awt / peer / gtk / GtkChoicePeer.java
1 /* GtkChoicePeer.java -- Implements ChoicePeer with GTK
2    Copyright (C) 1998, 1999, 2005  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package gnu.java.awt.peer.gtk;
40
41 import java.awt.Choice;
42 import java.awt.AWTEvent;
43 import java.awt.event.ItemEvent;
44 import java.awt.peer.ChoicePeer;
45
46 public class GtkChoicePeer extends GtkComponentPeer
47   implements ChoicePeer
48 {
49   private int selected;
50   
51   public GtkChoicePeer (Choice c)
52   {
53     super (c);
54
55     int count = c.getItemCount ();
56     if (count > 0)
57       {
58         for (int i = 0; i < count; i++)
59           add( c.getItem(i), i );
60
61         selected = c.getSelectedIndex();
62         if( selected >= 0 )
63           select( selected );
64       }
65     else
66       selected = -1;
67   }
68
69   native void create ();
70
71   native int nativeGetSelected ();
72
73   native void connectSignals ();
74
75   native void selectNative (int position);
76
77   native void selectNativeUnlocked (int position);
78
79   public native void add (String item, int index);
80
81   native void nativeRemove(int index);
82
83   native void nativeRemoveAll();
84
85   public void select (int position)
86   {
87     if (Thread.currentThread() == GtkMainThread.mainThread)
88       selectNativeUnlocked (position);
89     else
90       selectNative (position);
91   }
92
93   public void remove( int index )
94   {
95     // Ensure the triggering of an event when removing item zero if zero is the
96     // selected item, even though the selected index doesn't change.
97     if( index == 0 && selected == 0 )
98       selected = -1; 
99     nativeRemove( index );
100   }
101
102   public void removeAll()
103   {
104     selected = -1; // we do not want to trigger a select event here.
105     nativeRemoveAll();
106   }
107   
108   public void addItem (String item, int position)
109   {
110     add (item, position);
111   }
112
113   /**
114    * Callback from the native side on an item-select event, 
115    * which posts an event. The event is only posted if it represents an actual
116    * change. Selected is set to the peer's state initially, so that the
117    * first call to select(int) from the constructor will not trigger an event.
118    * (it should not)
119    */
120   protected void postChoiceItemEvent ( int index )
121   {
122     if( selected != index )
123       {
124         selected = index;
125         postItemEvent (((Choice) awtComponent).getItem( selected ), 
126                        ItemEvent.SELECTED);
127       }
128   }
129
130   /**
131    * Catches the event and calls Choice.select() if the component state
132    * needs updating.
133    */
134   public void handleEvent (AWTEvent event)
135   {
136     super.handleEvent( event );
137     if( event instanceof ItemEvent )
138       if( ((ItemEvent)event).getItemSelectable() == awtComponent &&
139           ((ItemEvent)event).getStateChange() == ItemEvent.SELECTED )
140         ((Choice)awtComponent).select( selected );
141   }
142 }
143