summaryrefslogtreecommitdiff
path: root/javax/swing/JComponent.java
blob: e78c0baff9d6a3c60943c2678d197e7b6f4ddac5 (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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
package javax.swing;

import java.awt.*;
import java.awt.peer.*;
import java.awt.event.*;
import java.io.*;

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

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

/**
 * Every component in swing inherits from this class (JLabel, JButton, etc).
 * It contains generic methods to manage events, properties and sizes.
 * Actual drawing of the component is channeled to a look-and-feel class
 * that is implemented elsewhere.
 *
 * @author Ronald Veldema (rveldema@cs.vu.nl)
 */

public abstract class JComponent extends Container implements Serializable
{
	Dimension pref,min,max;
	Border border;
	JToolTip tooltip;
	String tool_tip_text;
	boolean use_double_buffer, opaque;
	protected ComponentUI ui;

	Vector ancestor_list;
	Vector veto_list;
	Vector change_list;
	Hashtable prop_hash;

        public JComponent()
	{
		super();
		super.setLayout(new FlowLayout());
		
		//eventMask |= AWTEvent.COMP_KEY_EVENT_MASK;
		enableEvents( AWTEvent.KEY_EVENT_MASK );

		//updateUI(); // get a proper ui
	}


	// protected EventListenerList listenerList
	public boolean contains(int x, int y)
	{
		//return dims.contains(x,y);
		return super.contains(x,y);
	}


	public  void addNotify()
	{
		//Notification to this component that it now has a parent component.
		super.addNotify();
	}


	Hashtable get_prop_hash()
	{
		if (prop_hash == null)
			prop_hash = new Hashtable();
		return prop_hash;
	}
	public Vector get_veto_list()
	{
		if (veto_list == null)
			veto_list = new Vector();
		return veto_list;
	}
	public Vector get_change_list()
	{
		if (change_list == null)
			change_list = new Vector();
		return change_list;
	}
	public Vector get_ancestor_list()
	{
		if (ancestor_list == null)
			ancestor_list = new Vector();
		return ancestor_list;
	}

	public Object getClientProperty(Object key)
        {	return get_prop_hash().get(key);    }

	public void putClientProperty(Object key, Object value)
	{    get_prop_hash().put(key, value);   }


	public void removeAncestorListener(AncestorListener listener)
	{  get_ancestor_list().removeElement(listener);  }

        public void removePropertyChangeListener(PropertyChangeListener listener)
	{  get_change_list().removeElement(listener);   }

	public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
	{  /* FIXME */   get_change_list().removeElement(listener);   }

	public void removeVetoableChangeListener(VetoableChangeListener listener)
	{  get_veto_list().removeElement(listener);   }

	public void addAncestorListener(AncestorListener listener)
	{   get_ancestor_list().addElement(listener);  }

	public void addPropertyChangeListener(PropertyChangeListener listener)
	{  get_change_list().addElement(listener);   }

	public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
	{ /* FIXME */ get_change_list().addElement(listener);   }

	public void addVetoableChangeListener(VetoableChangeListener listener)
	{  get_veto_list().addElement(listener);    }

	public void computeVisibleRect(Rectangle rect)
	{
		//Returns the Component's "visible rect rectangle" - the intersection of the visible rectangles for this component and all of its ancestors.
		//super.computeVisibleRect(rect);
	}


	public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue)
	{
		//Reports a bound property change.
	}
	public void firePropertyChange(String propertyName, byte oldValue, byte newValue)
	{
		//    Reports a bound property change.
	}
	public void firePropertyChange(String propertyName, char oldValue, char newValue)
	{
		//Reports a bound property change.
	}

	public void firePropertyChange(String propertyName, double oldValue, double newValue)
	{
		//Reports a bound property change.
	}

	public void firePropertyChange(String propertyName, float oldValue, float newValue)
	{
		//       Reports a bound property change.
	}
	public void firePropertyChange(String propertyName, int oldValue, int newValue)
	{
		//       Reports a bound property change.
	}
	public void firePropertyChange(String propertyName, long oldValue, long newValue)
	{
		//Reports a bound property change. protected
	}

	public void firePropertyChange(String propertyName, Object oldValue, Object newValue)
	{
		//       Support for reporting bound property changes.
	}
	public void firePropertyChange(String propertyName, short oldValue, short newValue)
	{
		//       Reports a bound property change.
	}


	protected  void fireVetoableChange(String propertyName, Object oldValue, Object newValue)
	{
		//       Support for reporting constrained property changes.
	}

        public AccessibleContext getAccessibleContext()
	{
		//       Get the AccessibleContext associated with this JComponent
		return null;
	}
	
        public ActionListener getActionForKeyStroke(KeyStroke aKeyStroke)
	{
		//Return the object that will perform the action registered for a given keystroke.
		return null;
	}
	public float getAlignmentX()
	{
		//    Overrides Container.getAlignmentX to return the vertical alignment.
		return 0;
	}

	public float getAlignmentY()
	{
		//       Overrides Container.getAlignmentY to return the horizontal alignment.
		return 0;
	}
	public boolean getAutoscrolls()
	{
		//Returns true if this component automatically scrolls its contents when dragged, (when contained in a component that supports scrolling, like JViewport
		return false;
	}

	public void setBorder(Border border)
	{
		//System.out.println("set border called !, new border = " + border);
		this.border = border;
		revalidate();
		repaint();
	}

	public Border getBorder()
	{	return border;    }


        public Rectangle getBounds(Rectangle rv)
	{
		if (rv == null)
			return new Rectangle(getX(),getY(),getWidth(),getHeight());
		else
		{
			rv.setBounds(getX(),getY(),getWidth(),getHeight());
			return rv;
		}
	}

	protected  Graphics getComponentGraphics(Graphics g)
	{      return g;       }

	public int getConditionForKeyStroke(KeyStroke aKeyStroke)
	{
		//Return the condition that determines whether a registered action occurs in response to the specified keystroke.
		return 0;
	}
	public int getDebugGraphicsOptions()
	{
		return 0;
	}

	public Graphics getGraphics()
	{	return super.getGraphics();    }


	//    static MantaNative void DebugMe(Border b);

	public Insets getInsets()
	{
		//	System.out.println("watch this border");
		//	DebugMe(border);
		//	System.out.println("border = " + border);

		if (border == null)
		{
			//System.out.println("compares to null !");
			return super.getInsets();
		}
		//	System.out.println("compare failed !");
		return getBorder().getBorderInsets(this);
	}

	public Insets getInsets(Insets insets)
	{
	    Insets t = getInsets();

	    if (insets == null)
		return t;
	    
	    
	    return new Insets(t.top, t.left, t.bottom, t.right);
	}
	public Point getLocation(Point rv)
	{
		//Store the x,y origin of this component into "return value" rv and return rv.

		if (rv == null)
			return new Point(getX(),
					 getY());

		rv.setLocation(getX(),
		               getY());
		return rv;
	}

	public Dimension getMaximumSize()
	{
		if (max != null)
		{
			//System.out.println("HAVE_MAX_SIZE =  " + max);
			return max;
		}
		if (ui != null)
		{
		    Dimension s = ui.getMaximumSize(this);
		    if (s != null)
			{
				//System.out.println("        UI-MAX = " + s + ", UI = " + ui + ", IM="+this);
				return s;
			}
		}
		Dimension p = super.getMaximumSize();
		//System.out.println("               MAX = " + p + ", COMP="+this);
		return p;
	}

	public Dimension getMinimumSize()
	{
		if (min != null)
		{
			//System.out.println("HAVE_MIN_SIZE =  " + min);
			return min;
		}
		if (ui != null)
		{
			Dimension s = ui.getMinimumSize(this);
			if (s != null)
			{
				//	System.out.println("        UI-MIN = " + s + ", UI = " + ui + ", IM="+this);
				return s;
			}
		}
		Dimension p = super.getMinimumSize();
		//	System.out.println("              MIN = " + p + ", COMP="+this);
		return p;
	}

	public Dimension getPreferredSize()
	{
		if (pref != null)
		{
			//System.out.println("HAVE_PREF_SIZE =  " + pref);
			return pref;
		}

		if (ui != null)
		{
			Dimension s = ui.getPreferredSize(this);
			if (s != null)
			{
				//System.out.println("        UI-PREF = " + s + ", UI = " + ui + ", IM="+this);
				return s;
			}
		}
		Dimension p = super.getPreferredSize();
		//	System.out.println("              PREF = " + p + ", COMP="+this);
		return p;
	}

	public Component getNextFocusableComponent()
	{
		//          Return the next focusable component or null if the focus manager should choose the next focusable component automatically
		return null;
	}


	public KeyStroke[] getRegisteredKeyStrokes()
	{
		//          Return the KeyStrokes that will initiate registered actions.
		return null;
	}

	public JRootPane getRootPane()
	{
		JRootPane p = SwingUtilities.getRootPane(this);
		System.out.println("root = " + p);
		return p;
	}

	public Dimension getSize(Dimension rv)
	{
		//	System.out.println("JComponent, getsize()");
		if (rv == null)
			return new Dimension(getWidth(),
			                     getHeight());
		else
		{
			rv.setSize(getWidth(),
			           getHeight());
			return rv;
		}
	}



	/*********************************************************************
	 *
	 *
	 *  tooltips:
	 *
	 *
	 **************************************/

	public JToolTip createToolTip()
	{
		if (tooltip == null)
			tooltip = new JToolTip(tool_tip_text);
		return tooltip;
	}

	public Point getToolTipLocation(MouseEvent event)
        {	return null;    }

	public void setToolTipText(String text)
	{	tool_tip_text = text;    }

	public String getToolTipText()
	{	return tool_tip_text;    }

	public String getToolTipText(MouseEvent event)
	{	return tool_tip_text;    }

	/*********************************************************************
	 *
	 *
	 *    things to do with visibility:
	 *
	 *
	 **************************************/


	public Container getTopLevelAncestor()
	{
		//      Returns the top-level ancestor of this component (either the containing Window or Applet), or null if this component has not been added to any container.
		System.out.println("JComponent, getTopLevelAncestor()");
		return null;
	}

	public Rectangle getVisibleRect()
	{
		///    Returns the Component's "visible rectangle" - the intersection of this components visible rectangle:
		System.out.println("JComponent, getVisibleRect()");
		return null;
	}

	public void grabFocus()
	{
		//      Set the focus on the receiving component.
	}

	public boolean hasFocus()
	{
		//      Returns true if this Component has the keyboard focus.
		return false;
	}

	public boolean isDoubleBuffered()
	{	return use_double_buffer;    }

	public boolean isFocusCycleRoot()
	{
		//      Override this method and return true if your component is the root of of a component tree with its own focus cycle.
		return false;
	}

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

	public static boolean isLightweightComponent(Component c)
	{
		return c.getPeer() instanceof LightweightPeer;
	}

	public boolean isManagingFocus()
	{
		//      Override this method and return true if your JComponent manages focus.
		return false;
	}

        public boolean isOpaque()
	{	return opaque;    }

	public boolean isOptimizedDrawingEnabled()
	{
		//      Returns true if this component tiles its children,
		return true;
	}

	public boolean isPaintingTile()
	{
		//      Returns true if the receiving component is currently painting a tile.
		return false;
	}

	public boolean isRequestFocusEnabled()
	{
		//      Return whether the receiving component can obtain the focus by calling requestFocus
		return false;
	}

	public boolean isValidateRoot()
	{
		//      If this method returns true, revalidate() calls by descendants of this component will cause the entire tree beginning with this root to be validated.
		return false;
	}

	public void paint(Graphics g)
	{
		//	System.out.println("SWING_PAINT:" + this);

		paintBorder(g);
		paintComponent(g);
		paintChildren(g);
	}

	protected  void paintBorder(Graphics g)
	{
		//	System.out.println("PAINT_BORDER      x XXXXXXX x x x x x x x x x x x x:" + getBorder() + ", THIS="+this);

		//       Paint the component's border.
		if (getBorder() != null)
		{
			//System.out.println("PAINT_BORDER      x XXXXXXX x x x x x x x x x x x x:" + getBorder() + ", THIS="+this);

			getBorder().paintBorder(this,
			                        g,
			                        0,
			                        0,
			                        getWidth(),
			                        getHeight());
		}
	}

	protected  void paintChildren(Graphics g)
	{
	    //      Paint this component's children.
	    //super.paintChildren(g);
	}

	protected  void paintComponent(Graphics g)
	{
		//      If the UI delegate is non-null, call its paint method.
		if (ui != null)
		{
			ui.paint(g, this);
		}
	}
    
    /**
     * Paint the specified region in this component and all of 
     * its descendants that overlap the region, immediately.
     */
	public void paintImmediately(int x, int y, int w, int h)
        {
	
	    //Ronald: this shoudld probably redirect to the PLAF ....
	}

	public void paintImmediately(Rectangle r)
	{
	    ///      Paint the specified region now.
	    paintImmediately((int)r.getX(),
			     (int)r.getY(),
			     (int)r.getWidth(),
			     (int)r.getHeight());
	}
	protected  String paramString()
	{
		//      Returns a string representation of this JComponent.
		return "JComponent";
	}
	protected  void processComponentKeyEvent(KeyEvent e)
	{
		//     Process any key events that the component itself recognizes.
	    //System.out.println("COMP_KEY-EVENT: " + e);
	}
	protected  void processFocusEvent(FocusEvent e)
	{
		//      Processes focus events occurring on this component by dispatching them to any registered FocusListener objects.
	    //System.out.println("FOCUS_EVENT: " + e);
	}

	protected  void processKeyEvent(KeyEvent e)
	{
		//      Override processKeyEvent to process events protected
	    //System.out.println("KEY-EVENT: " + e);
	}

        public void processMouseMotionEvent(MouseEvent e)
	{
	    //      Processes mouse motion events occurring on this component by dispatching them to any registered MouseMotionListener objects.
	    //System.out.println("COMP_MOUSE-EVENT: " + e + ", MEMORY = " + Runtime.getRuntime().freeMemory());
	}

	public void registerKeyboardAction(ActionListener anAction,
	                            KeyStroke aKeyStroke,
	                            int aCondition)
	{
		registerKeyboardAction(anAction,
		                       null,
		                       aKeyStroke,
		                       aCondition);
	}

	public void registerKeyboardAction(ActionListener anAction,
	                            String aCommand,
	                            KeyStroke aKeyStroke,
	                            int aCondition)
	{
		//  Register a new keyboard action.
	}


	public void removeNotify()
	{
		//      Notification to this component that it no longer has a parent component.
	}

	public void repaint(long tm, int x, int y, int width, int height)
	{
		//   Adds the specified region to the dirty region list if the component is showing.
		//System.out.println("JC: repaint");
		super.repaint(tm, x,y,width,height);
	}

	public void repaint(Rectangle r)
	{
		//      Adds the specified region to the dirty region list if the component is showing.
		repaint((long)0,
		        (int)r.getX(),
		        (int)r.getY(),
		        (int)r.getWidth(),
		        (int)r.getHeight());
	}

	public boolean requestDefaultFocus()
	{
		//      Request the focus for the component that should have the focus by default.
		return false;
	}

	public void requestFocus()
	{
		//      Set focus on the receiving component if isRequestFocusEnabled returns true
		super.requestFocus();
	}

	public void resetKeyboardActions()
	{
		//      Unregister all keyboard actions
	}

	public void reshape(int x, int y, int w, int h)
	{
		///      Moves and resizes this component.
		super.reshape(x,y,w,h);
	}

	public void revalidate()
	{
		//     Support for deferred automatic layout.
		if (getParent() == null)
			invalidate();
	}

	public void scrollRectToVisible(Rectangle aRect)
	{
		//      Forwards the scrollRectToVisible() message to the JComponent's parent.
	}

	public void setAlignmentX(float alignmentX)
	{
		//      Set the the vertical alignment.
	}

	public void setAlignmentY(float alignmentY)
	{
		//      Set the the horizontal alignment.
	}

	public void setAutoscrolls(boolean autoscrolls)
	{
		//      If true this component will automatically scroll its contents when dragged, if contained in a component that supports scrolling, such as JViewport
	}


	public void setDebugGraphicsOptions(int debugOptions)
	{
		//      Enables or disables diagnostic information about every graphics operation performed within the component or one of its children.
	}

	public void setDoubleBuffered(boolean aFlag)
	{
		use_double_buffer = aFlag;
	}

	public void setEnabled(boolean enabled)
	{
		// Sets whether or not this component is enabled.
		super.setEnabled(enabled);
		repaint();
	}

	public void setFont(Font font)
	{
		super.setFont(font);
		revalidate();
		repaint();
	}
	public void setBackground(Color bg)
	{
		super.setBackground(bg);
		revalidate();
		repaint();
	}
	public void setForeground(Color fg)
	{
		super.setForeground(fg);
		revalidate();
		repaint();
	}

	public void setMaximumSize(Dimension maximumSize)
	{	max = maximumSize;    }

	public void setMinimumSize(Dimension minimumSize)
	{   min = minimumSize; }

	public void setPreferredSize(Dimension preferredSize)
	{   pref = preferredSize;   }

	public void setNextFocusableComponent(Component aComponent)
	{
		//       Specifies the next component to get the focus after this one, for example, when the tab key is pressed.
	}

	public void setOpaque(boolean isOpaque)
	{
		opaque = isOpaque;
		revalidate();
		repaint();
	}


	public void setRequestFocusEnabled(boolean aFlag)
	{
	}


	public void setVisible(boolean aFlag)
	{
		//    Makes the component visible or invisible.

		super.setVisible(aFlag);
		if (getParent() != null)
		{
			Rectangle dims = getBounds();
			getParent().repaint((int)dims.getX(),
			                    (int)dims.getY(),
			                    (int)dims.getWidth(),
			                    (int)dims.getHeight());
		}
	}

	public void unregisterKeyboardAction(KeyStroke aKeyStroke)
	{
		//          Unregister a keyboard action.
	}


	public void update(Graphics g)
	{
		paint(g);
	}



	/******************************************
	 *
	 *
	 *  UI management
	 * 
	 *
	 *********/

        public String getUIClassID()
	{
		///          Return the UIDefaults key used to look up the name of the swing.
		return "JComponent";
	}

	protected void setUI(ComponentUI newUI)
	{
		if (ui != null)
		{
			ui.uninstallUI(this);
		}

		//          Set the look and feel delegate for this component.
		ui = newUI;

		if (ui != null)
		{
			ui.installUI(this);
		}

		revalidate();
		repaint();
	}

	public void updateUI()
	{
		//        Resets the UI property to a value from the current look and feel.
		System.out.println("update UI not overwritten in class: " + this);
	}

}