53 public final static int VERSCROLLBAR = 1;
54 public final static int HORSCROLLBAR = 2;
55 public final static int DEFAULT_VISIBLE_ROWS = 4; // From java.awt.List,
56 public final static int HORIZ_SCROLL_AMT = 10;
57
58 private final static int PAINT_VSCROLL = 2;
59 private final static int PAINT_HSCROLL = 4;
60 private final static int PAINT_ITEMS = 8;
61 private final static int PAINT_FOCUS = 16;
62 private final static int PAINT_BACKGROUND = 32;
63 private final static int PAINT_HIDEFOCUS = 64;
64 private final static int PAINT_ALL =
65 PAINT_VSCROLL | PAINT_HSCROLL | PAINT_ITEMS | PAINT_FOCUS | PAINT_BACKGROUND;
66 private final static int COPY_AREA = 128;
67
68 XVerticalScrollbar vsb;
69 XHorizontalScrollbar hsb;
70 ListPainter painter;
71
72 // TODO: ick - Vector?
73 Vector items;
74 boolean multipleSelections;
75 int active = NONE;
76
77 // Holds the array of the indexes of the elements which is selected
78 // This array should be kept sorted, low to high.
79 int selected[];
80 int fontHeight;
81 int fontAscent;
82 int fontLeading;
83
84 // Holds the index of the item used in the previous operation (selectItem, deselectItem)
85 // Adding of an item or clearing of the list sets this index to -1
86 // The index is used at the moment of the post of ACTION_PERFORMED event after the mouse double click event.
87 int currentIndex = -1;
88
89 // Used for tracking selection/deselection between mousePress/Release
90 // and for ItemEvents
91 int eventIndex = -1;
92 int eventType = NONE;
93
122 boolean isScrollBarOriginated = false;
123
124 // This variable is set to true after the "mouse pressed" event and to false after the "mouse released" event
125 // Fixed 6293432: Key events ('SPACE', 'UP', 'DOWN') aren't blocked if mouse is kept in 'PRESSED' state for List, XAWT
126 boolean isMousePressed = false;
127
128 /**
129 * Create a list
130 */
131 XListPeer(List target) {
132 super(target);
133 }
134
135 /**
136 * Overridden from XWindow
137 */
138 public void preInit(XCreateWindowParams params) {
139 super.preInit(params);
140
141 // Stuff that must be initialized before layout() is called
142 items = new Vector();
143 createVerScrollbar();
144 createHorScrollbar();
145
146 painter = new ListPainter();
147
148 // See 6246467 for more information
149 bgColorSet = target.isBackgroundSet();
150 fgColorSet = target.isForegroundSet();
151 }
152
153 public void postInit(XCreateWindowParams params) {
154 super.postInit(params);
155 initFontMetrics();
156 // TODO: more efficient way?
157 // do we really want/need a copy of all the items?
158 // get all items from target
159 List l = (List)target;
160 int stop = l.getItemCount();
161 for (int i = 0 ; i < stop; i++) {
162 items.addElement(l.getItem(i));
264
265 /**
266 * Calculate font metrics
267 */
268 void initFontMetrics() {
269 FontMetrics fm = getFontMetrics(getFont());
270 fontHeight = fm.getHeight();
271 fontAscent = fm.getAscent();
272 fontLeading = fm.getLeading();
273 }
274
275
276 /**
277 * return the length of the largest item in the list
278 */
279 int maxLength() {
280 FontMetrics fm = getFontMetrics(getFont());
281 int m = 0;
282 int end = items.size();
283 for(int i = 0 ; i < end ; i++) {
284 int l = fm.stringWidth(((String)items.elementAt(i)));
285 m = Math.max(m, l);
286 }
287 return m;
288 }
289
290 /**
291 * Calculates the width of item's label
292 */
293 int getItemWidth(int i) {
294 FontMetrics fm = getFontMetrics(getFont());
295 return fm.stringWidth((String)items.elementAt(i));
296 }
297
298 /**
299 * return the on-screen width of the given string "str"
300 */
301 int stringLength(String str) {
302 FontMetrics fm = getFontMetrics(target.getFont());
303 return fm.stringWidth(str);
304 }
305
306 public void setForeground(Color c) {
307 fgColorSet = true;
308 super.setForeground(c);
309 }
310
311 public void setBackground(Color c) {
312 bgColorSet = true;
313 super.setBackground(c);
314 }
315
642 }
643 }
644 void mouseReleased(MouseEvent mouseEvent) {
645 if (isEnabled() && mouseEvent.getButton() == MouseEvent.BUTTON1) {
646 //winReleaseCursorFocus();
647 int clickCount = mouseEvent.getClickCount();
648 if (active == VERSCROLLBAR) {
649 vsb.handleMouseEvent(mouseEvent.getID(),
650 mouseEvent.getModifiers(),
651 mouseEvent.getX()-(width-SCROLLBAR_WIDTH),
652 mouseEvent.getY());
653 } else if(active == HORSCROLLBAR) {
654 hsb.handleMouseEvent(mouseEvent.getID(),
655 mouseEvent.getModifiers(),
656 mouseEvent.getX(),
657 mouseEvent.getY()-(height-SCROLLBAR_WIDTH));
658 } else if ( ( currentIndex >= 0 ) && ( clickCount >= 2 ) &&
659 ( clickCount % 2 == 0 ) ) {
660 postEvent(new ActionEvent(target,
661 ActionEvent.ACTION_PERFORMED,
662 (String)items.elementAt(currentIndex),
663 mouseEvent.getWhen(),
664 mouseEvent.getModifiers())); // No ext mods
665 } else if (active == WINDOW) {
666 // See 6243382 for more information
667 trackMouseReleasedScroll();
668
669 if (eventType == ItemEvent.DESELECTED) {
670 assert multipleSelections : "Shouldn't get a deselect for a single-select List";
671 // Paint deselection the release
672 deselectItem(eventIndex);
673 }
674 if (eventType != NONE) {
675 postEvent(new ItemEvent((List)target,
676 ItemEvent.ITEM_STATE_CHANGED,
677 Integer.valueOf(eventIndex),
678 eventType));
679 }
680 }
681 active = NONE;
682 eventIndex = -1;
969 // That is, pressing space bar on a
970 // single-select list when the focused
971 // item is already selected does NOT
972 // send an ItemEvent.SELECTED event.
973 selectItem(getFocusIndex());
974 postEvent(new ItemEvent((List)target,
975 ItemEvent.ITEM_STATE_CHANGED,
976 Integer.valueOf(getFocusIndex()),
977 ItemEvent.SELECTED));
978 }
979 break;
980 case KeyEvent.VK_ENTER:
981 // It looks to me like there are bugs as well as inconsistencies
982 // in the way the Enter key is handled by both Solaris and Windows.
983 // So for now in XAWT, I'm going to simply go by what the List docs
984 // say: "AWT also generates an action event when the user presses
985 // the return key while an item in the list is selected."
986 if (selected.length > 0) {
987 postEvent(new ActionEvent((List)target,
988 ActionEvent.ACTION_PERFORMED,
989 (String)items.elementAt(getFocusIndex()),
990 e.getWhen(),
991 e.getModifiers())); // ActionEvent doesn't have
992 // extended modifiers.
993 }
994 break;
995 }
996 }
997
998 /**
999 * return value from the scrollbar
1000 */
1001 public void notifyValue(XScrollbar obj, int type, int v, boolean isAdjusting) {
1002
1003 if (log.isLoggable(PlatformLogger.Level.FINE)) {
1004 log.fine("Notify value changed on " + obj + " to " + v);
1005 }
1006 int value = obj.getValue();
1007 if (obj == vsb) {
1008 scrollVertical(v - value);
1009
1326 return;
1327 }
1328 if (isItemHidden(index)) { // Do I really need to call this?
1329 // If index is above the top, scroll up
1330 if (index < vsb.getValue()) {
1331 scrollVertical(index - vsb.getValue());
1332 }
1333 // If index is below the bottom, scroll down
1334 else if (index > lastItemDisplayed()) {
1335 int val = index - lastItemDisplayed();
1336 scrollVertical(val);
1337 }
1338 }
1339 }
1340
1341 /**
1342 * clear
1343 */
1344 public void clear() {
1345 selected = new int[0];
1346 items = new Vector();
1347 currentIndex = -1;
1348 // Fixed 6291736: ITEM_STATE_CHANGED triggered after List.removeAll(), XToolkit
1349 // We should update 'focusIndex' variable more carefully
1350 setFocusIndex(-1);
1351 vsb.setValue(0);
1352 maxLength = 0;
1353 layout();
1354 repaint();
1355 }
1356
1357 /**
1358 * return the selected indexes
1359 */
1360 public int[] getSelectedIndexes() {
1361 return selected;
1362 }
1363
1364 /**
1365 * return the y value of the given index "i".
1366 * the y value represents the top of the text
1909 if (log.isLoggable(PlatformLogger.Level.FINEST)) {
1910 log.finest("Painted item is selected");
1911 }
1912 g.setColor(getListForeground());
1913 } else {
1914 g.setColor(getListBackground());
1915 }
1916 if (log.isLoggable(PlatformLogger.Level.FINEST)) {
1917 log.finest("Filling " + new Rectangle(x, y, w, h));
1918 }
1919 g.fillRect(x, y, w, h);
1920
1921 if (index <= getLastVisibleItem() && index < items.size()) {
1922 if (!isEnabled()){
1923 g.setColor(getDisabledColor());
1924 } else if (isSelected(index)) {
1925 g.setColor(getListBackground());
1926 } else {
1927 g.setColor(getListForeground());
1928 }
1929 String str = (String)items.elementAt(index);
1930 g.drawString(str, x - hsb.getValue(), y + fontAscent);
1931 } else {
1932 // Clear the remaining area around the item - focus area and the rest of border
1933 g.setClip(x, y, listWidth, h);
1934 g.setColor(getListBackground());
1935 g.fillRect(x, y, listWidth, h);
1936 }
1937 g.setClip(clip);
1938 }
1939 }
1940
1941 void paintScrollBar(XScrollbar scr, Graphics g, int x, int y, int width, int height, boolean paintAll) {
1942 if (log.isLoggable(PlatformLogger.Level.FINEST)) {
1943 log.finest("Painting scrollbar " + scr + " width " +
1944 width + " height " + height + ", paintAll " + paintAll);
1945 }
1946 g.translate(x, y);
1947 scr.paint(g, getSystemColors(), paintAll);
1948 g.translate(-x, -y);
1949 }
|
53 public final static int VERSCROLLBAR = 1;
54 public final static int HORSCROLLBAR = 2;
55 public final static int DEFAULT_VISIBLE_ROWS = 4; // From java.awt.List,
56 public final static int HORIZ_SCROLL_AMT = 10;
57
58 private final static int PAINT_VSCROLL = 2;
59 private final static int PAINT_HSCROLL = 4;
60 private final static int PAINT_ITEMS = 8;
61 private final static int PAINT_FOCUS = 16;
62 private final static int PAINT_BACKGROUND = 32;
63 private final static int PAINT_HIDEFOCUS = 64;
64 private final static int PAINT_ALL =
65 PAINT_VSCROLL | PAINT_HSCROLL | PAINT_ITEMS | PAINT_FOCUS | PAINT_BACKGROUND;
66 private final static int COPY_AREA = 128;
67
68 XVerticalScrollbar vsb;
69 XHorizontalScrollbar hsb;
70 ListPainter painter;
71
72 // TODO: ick - Vector?
73 Vector<String> items;
74 boolean multipleSelections;
75 int active = NONE;
76
77 // Holds the array of the indexes of the elements which is selected
78 // This array should be kept sorted, low to high.
79 int selected[];
80 int fontHeight;
81 int fontAscent;
82 int fontLeading;
83
84 // Holds the index of the item used in the previous operation (selectItem, deselectItem)
85 // Adding of an item or clearing of the list sets this index to -1
86 // The index is used at the moment of the post of ACTION_PERFORMED event after the mouse double click event.
87 int currentIndex = -1;
88
89 // Used for tracking selection/deselection between mousePress/Release
90 // and for ItemEvents
91 int eventIndex = -1;
92 int eventType = NONE;
93
122 boolean isScrollBarOriginated = false;
123
124 // This variable is set to true after the "mouse pressed" event and to false after the "mouse released" event
125 // Fixed 6293432: Key events ('SPACE', 'UP', 'DOWN') aren't blocked if mouse is kept in 'PRESSED' state for List, XAWT
126 boolean isMousePressed = false;
127
128 /**
129 * Create a list
130 */
131 XListPeer(List target) {
132 super(target);
133 }
134
135 /**
136 * Overridden from XWindow
137 */
138 public void preInit(XCreateWindowParams params) {
139 super.preInit(params);
140
141 // Stuff that must be initialized before layout() is called
142 items = new Vector<>();
143 createVerScrollbar();
144 createHorScrollbar();
145
146 painter = new ListPainter();
147
148 // See 6246467 for more information
149 bgColorSet = target.isBackgroundSet();
150 fgColorSet = target.isForegroundSet();
151 }
152
153 public void postInit(XCreateWindowParams params) {
154 super.postInit(params);
155 initFontMetrics();
156 // TODO: more efficient way?
157 // do we really want/need a copy of all the items?
158 // get all items from target
159 List l = (List)target;
160 int stop = l.getItemCount();
161 for (int i = 0 ; i < stop; i++) {
162 items.addElement(l.getItem(i));
264
265 /**
266 * Calculate font metrics
267 */
268 void initFontMetrics() {
269 FontMetrics fm = getFontMetrics(getFont());
270 fontHeight = fm.getHeight();
271 fontAscent = fm.getAscent();
272 fontLeading = fm.getLeading();
273 }
274
275
276 /**
277 * return the length of the largest item in the list
278 */
279 int maxLength() {
280 FontMetrics fm = getFontMetrics(getFont());
281 int m = 0;
282 int end = items.size();
283 for(int i = 0 ; i < end ; i++) {
284 int l = fm.stringWidth(items.elementAt(i));
285 m = Math.max(m, l);
286 }
287 return m;
288 }
289
290 /**
291 * Calculates the width of item's label
292 */
293 int getItemWidth(int i) {
294 FontMetrics fm = getFontMetrics(getFont());
295 return fm.stringWidth(items.elementAt(i));
296 }
297
298 /**
299 * return the on-screen width of the given string "str"
300 */
301 int stringLength(String str) {
302 FontMetrics fm = getFontMetrics(target.getFont());
303 return fm.stringWidth(str);
304 }
305
306 public void setForeground(Color c) {
307 fgColorSet = true;
308 super.setForeground(c);
309 }
310
311 public void setBackground(Color c) {
312 bgColorSet = true;
313 super.setBackground(c);
314 }
315
642 }
643 }
644 void mouseReleased(MouseEvent mouseEvent) {
645 if (isEnabled() && mouseEvent.getButton() == MouseEvent.BUTTON1) {
646 //winReleaseCursorFocus();
647 int clickCount = mouseEvent.getClickCount();
648 if (active == VERSCROLLBAR) {
649 vsb.handleMouseEvent(mouseEvent.getID(),
650 mouseEvent.getModifiers(),
651 mouseEvent.getX()-(width-SCROLLBAR_WIDTH),
652 mouseEvent.getY());
653 } else if(active == HORSCROLLBAR) {
654 hsb.handleMouseEvent(mouseEvent.getID(),
655 mouseEvent.getModifiers(),
656 mouseEvent.getX(),
657 mouseEvent.getY()-(height-SCROLLBAR_WIDTH));
658 } else if ( ( currentIndex >= 0 ) && ( clickCount >= 2 ) &&
659 ( clickCount % 2 == 0 ) ) {
660 postEvent(new ActionEvent(target,
661 ActionEvent.ACTION_PERFORMED,
662 items.elementAt(currentIndex),
663 mouseEvent.getWhen(),
664 mouseEvent.getModifiers())); // No ext mods
665 } else if (active == WINDOW) {
666 // See 6243382 for more information
667 trackMouseReleasedScroll();
668
669 if (eventType == ItemEvent.DESELECTED) {
670 assert multipleSelections : "Shouldn't get a deselect for a single-select List";
671 // Paint deselection the release
672 deselectItem(eventIndex);
673 }
674 if (eventType != NONE) {
675 postEvent(new ItemEvent((List)target,
676 ItemEvent.ITEM_STATE_CHANGED,
677 Integer.valueOf(eventIndex),
678 eventType));
679 }
680 }
681 active = NONE;
682 eventIndex = -1;
969 // That is, pressing space bar on a
970 // single-select list when the focused
971 // item is already selected does NOT
972 // send an ItemEvent.SELECTED event.
973 selectItem(getFocusIndex());
974 postEvent(new ItemEvent((List)target,
975 ItemEvent.ITEM_STATE_CHANGED,
976 Integer.valueOf(getFocusIndex()),
977 ItemEvent.SELECTED));
978 }
979 break;
980 case KeyEvent.VK_ENTER:
981 // It looks to me like there are bugs as well as inconsistencies
982 // in the way the Enter key is handled by both Solaris and Windows.
983 // So for now in XAWT, I'm going to simply go by what the List docs
984 // say: "AWT also generates an action event when the user presses
985 // the return key while an item in the list is selected."
986 if (selected.length > 0) {
987 postEvent(new ActionEvent((List)target,
988 ActionEvent.ACTION_PERFORMED,
989 items.elementAt(getFocusIndex()),
990 e.getWhen(),
991 e.getModifiers())); // ActionEvent doesn't have
992 // extended modifiers.
993 }
994 break;
995 }
996 }
997
998 /**
999 * return value from the scrollbar
1000 */
1001 public void notifyValue(XScrollbar obj, int type, int v, boolean isAdjusting) {
1002
1003 if (log.isLoggable(PlatformLogger.Level.FINE)) {
1004 log.fine("Notify value changed on " + obj + " to " + v);
1005 }
1006 int value = obj.getValue();
1007 if (obj == vsb) {
1008 scrollVertical(v - value);
1009
1326 return;
1327 }
1328 if (isItemHidden(index)) { // Do I really need to call this?
1329 // If index is above the top, scroll up
1330 if (index < vsb.getValue()) {
1331 scrollVertical(index - vsb.getValue());
1332 }
1333 // If index is below the bottom, scroll down
1334 else if (index > lastItemDisplayed()) {
1335 int val = index - lastItemDisplayed();
1336 scrollVertical(val);
1337 }
1338 }
1339 }
1340
1341 /**
1342 * clear
1343 */
1344 public void clear() {
1345 selected = new int[0];
1346 items = new Vector<>();
1347 currentIndex = -1;
1348 // Fixed 6291736: ITEM_STATE_CHANGED triggered after List.removeAll(), XToolkit
1349 // We should update 'focusIndex' variable more carefully
1350 setFocusIndex(-1);
1351 vsb.setValue(0);
1352 maxLength = 0;
1353 layout();
1354 repaint();
1355 }
1356
1357 /**
1358 * return the selected indexes
1359 */
1360 public int[] getSelectedIndexes() {
1361 return selected;
1362 }
1363
1364 /**
1365 * return the y value of the given index "i".
1366 * the y value represents the top of the text
1909 if (log.isLoggable(PlatformLogger.Level.FINEST)) {
1910 log.finest("Painted item is selected");
1911 }
1912 g.setColor(getListForeground());
1913 } else {
1914 g.setColor(getListBackground());
1915 }
1916 if (log.isLoggable(PlatformLogger.Level.FINEST)) {
1917 log.finest("Filling " + new Rectangle(x, y, w, h));
1918 }
1919 g.fillRect(x, y, w, h);
1920
1921 if (index <= getLastVisibleItem() && index < items.size()) {
1922 if (!isEnabled()){
1923 g.setColor(getDisabledColor());
1924 } else if (isSelected(index)) {
1925 g.setColor(getListBackground());
1926 } else {
1927 g.setColor(getListForeground());
1928 }
1929 String str = items.elementAt(index);
1930 g.drawString(str, x - hsb.getValue(), y + fontAscent);
1931 } else {
1932 // Clear the remaining area around the item - focus area and the rest of border
1933 g.setClip(x, y, listWidth, h);
1934 g.setColor(getListBackground());
1935 g.fillRect(x, y, listWidth, h);
1936 }
1937 g.setClip(clip);
1938 }
1939 }
1940
1941 void paintScrollBar(XScrollbar scr, Graphics g, int x, int y, int width, int height, boolean paintAll) {
1942 if (log.isLoggable(PlatformLogger.Level.FINEST)) {
1943 log.finest("Painting scrollbar " + scr + " width " +
1944 width + " height " + height + ", paintAll " + paintAll);
1945 }
1946 g.translate(x, y);
1947 scr.paint(g, getSystemColors(), paintAll);
1948 g.translate(-x, -y);
1949 }
|