summaryrefslogtreecommitdiff
path: root/javax/swing/AbstractButton.java
blob: 71c37210f3e4b7469bb001dc59af37f86c107f35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package javax.swing;

import java.awt.*;
import java.awt.event.*;

import javax.swing.event.*;
import javax.swing.plaf.*;

import java.util.*;
import java.beans.*;


public abstract class AbstractButton extends JComponent
			implements ItemSelectable, SwingConstants
{
	Icon default_icon, pressed_button, disabled_button,
	selected_button, disabled_selected_button, current_icon;
	String text;

	int vert_align = CENTER;
	int hori_align = CENTER;
	int hori_text_pos = CENTER;
	int vert_text_pos = CENTER;

	boolean paint_border = true, paint_focus;
	Action action_taken;
	ButtonModel model;
	Insets margin;


	public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted";

	static private class JFocusListener implements FocusListener
	{
		AbstractButton c;

		JFocusListener(AbstractButton c)
		{
			this.c = c;
		}

		public void focusLost(FocusEvent event)
		{
			c.getModel().setArmed(false);

			System.out.println("LOST FOCUS");
			if (c.isFocusPainted())
			{
				c.repaint();
			}
		}
		public void focusGained(FocusEvent event)
		{
			System.out.println("GAIN FOCUS");
		}
	}


	/**********************************************
	 *
	 * 
	 *       Constructors
	 *
	 *
	 ****************/

	AbstractButton()
	{
		this("",null);
	}

	AbstractButton(String text,
	               Icon icon)
	{
		this.text    = text;
		setIcon(icon);

		setAlignmentX(LEFT_ALIGNMENT);
		setAlignmentY(CENTER_ALIGNMENT);

		addFocusListener( new JFocusListener(this) );

		setModel(new DefaultButtonModel(this));

		updateUI(); // get a proper ui
	}


	/**********************************************
	 *
	 * 
	 *       Actions etc
	 *
	 *
	 ****************/

	ButtonModel getModel()
	{	return model;    }
	void setModel(ButtonModel newModel)
	{	model = newModel;    }

	String getActionCommand()
	{	return getModel().getActionCommand();    }
	void setActionCommand(String aCommand)
	{   getModel().setActionCommand(aCommand);   }

	void addActionListener(ActionListener l)
	{	getModel().addActionListener(l);    }
	void removeActionListener(ActionListener l)
	{	getModel().removeActionListener(l);    }

	void addChangeListener(ChangeListener l)
	{   getModel().addChangeListener(l);     }
	void removeChangeListener(ChangeListener l)
	{  getModel().removeChangeListener(l);    }

        public void addItemListener(ItemListener l)
	{  getModel().addItemListener(l);    }
        public void removeItemListener(ItemListener l)
	{  getModel().removeItemListener(l);  }

	int getHorizontalAlignment()
	{	return hori_align;    }
	int getHorizontalTextPosition()
	{	return hori_text_pos;    }
	int getVerticalAlignment()
	{	return vert_align;   }
	int getVerticalTextPosition()
	{	return vert_text_pos;  }


	protected  void fireItemStateChanged(ItemEvent event)
	{    getModel().fireItemStateChanged(event);    }
	protected  void fireStateChanged(ChangeEvent event)
	{	 getModel().fireStateChanged(event);    }
	protected void fireActionPerformed(ActionEvent event)
	{	getModel().fireActionPerformed(event);    }

	void setVerticalAlignment(int alignment)
	{	vert_align = alignment;    }
	void setHorizontalAlignment(int alignment)
	{   hori_align = alignment;   }
	void setVerticalTextPosition(int textPosition)
	{	vert_text_pos = textPosition;    }
	void setHorizontalTextPosition(int textPosition)
	{   hori_text_pos = textPosition;   }

	int getMnemonic()
	{	return getModel().getMnemonic();    }
	void setMnemonic(char mne)
	{	getModel().setMnemonic(mne);    }
	void setMnemonic(int mne)
	{	getModel().setMnemonic(mne);    }

	void setRolloverEnabled(boolean b)
	{    getModel().setRollover(b);    }
	boolean isRolloverEnabled()
	{    return getModel().isRollover();     }


	boolean isBorderPainted()
	{	return paint_border;    }
	void setBorderPainted(boolean b)
	{
		if (b != paint_border)
		{
			paint_border = b;
			revalidate();
			repaint();
		}
	}

	Action getAction()
	{	return action_taken;    }
	void setAction(Action a)
	{
		action_taken = a;
		revalidate();
		repaint();
	}

	void setSelected(boolean b)
	{	getModel().setSelected(b);    }
	boolean isSelected()
	{	return getModel().isSelected();     }


	Icon getIcon()
	{	return default_icon;    }
	void setIcon(Icon defaultIcon)
	{
		if (default_icon == defaultIcon)
			return;

		default_icon = defaultIcon;
		if (default_icon != null)
		{
			default_icon.setParent(this);
		}
		revalidate();
		repaint();
	}

	String getText()
	{	return text;    }
	void setLabel(String label)
	{	setText(label);    }
	String getLabel()
	{	return getText();    }
	void setText(String text)
	{
		this.text = text;
		revalidate();
		repaint();
	}


	Insets getMargin()
	{      return margin; }
	void setMargin(Insets m)
	{
		margin = m;
		revalidate();
		repaint();
	}

	public void setEnabled(boolean b)
	{
		super.setEnabled(b);
		getModel().setEnabled(b);
		repaint();
	}

	Icon getPressedIcon()
	{	return pressed_button;    }
	void setPressedIcon(Icon pressedIcon)
	{
		pressed_button = pressedIcon;
		revalidate();
		repaint();
	}


	Icon getDisabledIcon()
	{	return disabled_button;    }
	void setDisabledIcon(Icon disabledIcon)
	{
		disabled_button = disabledIcon;
		revalidate();
		repaint();
	}

	public boolean isFocusPainted()
	{   return paint_focus;   }
	public void setFocusPainted(boolean b)
	{
		boolean old = paint_focus;
		paint_focus = b;

		firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY,
		                   old,
		                   b);
		if (hasFocus())
		{
			revalidate();
			repaint();
		}
	}

	public boolean isFocusTraversable()
	{
		//Identifies whether or not this component can receive the focus.
		return true;
	}


	protected  int checkHorizontalKey(int key, String exception)
	{
		//       Verify that key is a legal value for the horizontalAlignment properties.
		return 0;
	}
	protected  int checkVerticalKey(int key, String exception)
	{
		//       Ensures that the key is a valid.
		return 0;
	}
	protected  void configurePropertiesFromAction(Action a)
	{
		//Factory method which sets the ActionEvent source's properties according to values from the Action instance.
	}

	protected  ActionListener createActionListener()
	{
		return new ActionListener()
		       {
			       public void actionPerformed(ActionEvent e) { }
		       };
	}

	protected  PropertyChangeListener createActionPropertyChangeListener(Action a)
	{
		//Factory method which creates the PropertyChangeListener used to update the ActionEvent source as properties change on its Action instance.
		return null;
	}
	protected  ChangeListener createChangeListener()
	{
		//       Subclasses that want to handle ChangeEvents differently can override this to return another ChangeListener implementation.
		return new ChangeListener()
		       {
			       public void stateChanged(ChangeEvent e) { }
		       };
	}

	protected  ItemListener createItemListener()
	{
		return new ItemListener()
		       {
			       public void itemStateChanged(ItemEvent e) { }
		       };
	}


	void doClick()
	{
		doClick(100);
	}
	void doClick(int pressTime)
	{
		Toolkit.tlkBeep ();
		//Programmatically perform a "click".
	}


	Icon getDisabledSelectedIcon()
	{
		//Returns the icon used by the button when it's disabled and selected.
		return disabled_selected_button;
	}


	Icon getRolloverIcon()
	{
		//       Returns the rollover icon for the button.
		return null;
	}

	Icon getRolloverSelectedIcon()
	{
		//       Returns the rollover selection icon for the button.
		return null;
	}
	Icon getSelectedIcon()
	{
		//       Returns the selected icon for the button.
		return selected_button;
	}


	public Object[] getSelectedObjects()
	{
		//Returns an array (length 1) containing the label or null if the button is not selected.
		return null;
	}


	public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
	{
		//This is overridden to return false if the current Icon's Image is not equal to the passed in Image img.
		return current_icon == img;
	}

	boolean isContentAreaFilled()
	{
		//       Checks whether the "content area" of the button should be filled.
		return false;
	}



	protected  void paintBorder(Graphics g)
	{
		//       Paint the button's border if BorderPainted property is true.
		if (isBorderPainted())
			super.paintBorder(g);
	}
	protected  String paramString()
	{
		//        Returns a string representation of this AbstractButton.
		return "AbstractButton";
	}


	void setContentAreaFilled(boolean b)
	{
		//Sets whether the button should paint the content area or leave it transparent.
	}


	void setDisabledSelectedIcon(Icon disabledSelectedIcon)
	{
		//          Sets the disabled selection icon for the button.
	}

	void setRolloverIcon(Icon rolloverIcon)
	{
		//       Sets the rollover icon for the button.
	}
	void setRolloverSelectedIcon(Icon rolloverSelectedIcon)
	{
		//       Sets the rollover selected icon for the button.
	}


	void setSelectedIcon(Icon selectedIcon)
	{
		//       Sets the selected icon for the button.
	}


	public void setUI(ButtonUI ui)
	{	//       Sets the L&F object that renders this component.
		super.setUI(ui);
	}

	ButtonUI getUI()
	{
		//Returns the L&F object that renders this component.
		return (ButtonUI) ui;
	}

	void updateUI()
	{
		/*
		  //          Notification from the UIFactory that the L&F has changed.
		  if (getUI() == null)
		  {
		  setUI(getUI());
		  }
		*/
	}

	protected void processActionEvent(ActionEvent e)
	{
		System.out.println("PROCESS-ACTION-EVENT: " + e);
	}


	protected void processMouseEvent(MouseEvent e)
	{
		//	System.out.println("PROCESS-MOUSE-EVENT: " + e + ", PRESSED-IN-MODEL="+getModel().isPressed());

		switch (e.getID())
		{
		case MouseEvent.MOUSE_MOVED:
			{
				break;
			}
		case MouseEvent.MOUSE_PRESSED:
			{
				if (! isEnabled())
				{
					System.out.println("button not enabled, ignoring press");
				}
				else
				{
					System.out.println("telling model:press: " + getModel());
					getModel().setPressed(true);
					repaint();
				}
				break;
			}

		case MouseEvent.MOUSE_RELEASED:
			{
				if (! isEnabled())
				{
					System.out.println("button not enabled, ignoring release");
				}
				else
				{
					int flags = 0;

					System.out.println("        XXX--> " + getActionCommand());

					fireActionPerformed(new ActionEvent(this,
					                                    ActionEvent.ACTION_PERFORMED,
					                                    getActionCommand(),
					                                    flags));

					//System.out.println("telling model:release");
					getModel().setPressed(false);
					repaint();
				}
				break;
			}
		case MouseEvent.MOUSE_CLICKED:
			{
				break;
			}
		}
	}
}