1. /*
  2. * @(#)Region.java 1.30 04/02/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.synth;
  8. import javax.swing.*;
  9. import java.util.*;
  10. /**
  11. * A distinct rendering area of a Swing component. A component may
  12. * support one or more regions. Specific component regions are defined
  13. * by the typesafe enumeration in this class.
  14. * <p>
  15. * Regions are typically used as a way to identify the <code>Component</code>s
  16. * and areas a particular style is to apply to. Synth's file format allows you
  17. * to bind styles based on the name of a <code>Region</code>.
  18. * The name is derived from the field name of the constant:
  19. * <ol>
  20. * <li>Map all characters to lowercase.
  21. * <li>Map the first character to uppercase.
  22. * <li>Map the first character after underscores to uppercase.
  23. * <li>Remove all underscores.
  24. * </ol>
  25. * For example, to identify the <code>SPLIT_PANE</code>
  26. * <code>Region</code> you would use <code>SplitPane</code>.
  27. * The following shows a custom <code>SynthStyleFactory</code>
  28. * that returns a specific style for split panes:
  29. * <pre>
  30. * public SynthStyle getStyle(JComponent c, Region id) {
  31. * if (id == Region.SPLIT_PANE) {
  32. * return splitPaneStyle;
  33. * }
  34. * ...
  35. * }
  36. * </pre>
  37. * The following <a href="doc-files/synthFileFormat.html">xml</a>
  38. * accomplishes the same thing:
  39. * <pre>
  40. * <style id="splitPaneStyle">
  41. * ...
  42. * </style>
  43. * <bind style="splitPaneStyle" type="region" key="SplitPane"/>
  44. * </pre>
  45. *
  46. * @version 1.30, 02/19/04
  47. * @since 1.5
  48. * @author Scott Violet
  49. */
  50. public class Region {
  51. private static final Map uiToRegionMap = new HashMap();
  52. private static final Map lowerCaseNameMap = new HashMap();
  53. /**
  54. * ArrowButton's are special types of buttons that also render a
  55. * directional indicator, typically an arrow. ArrowButtons are used by
  56. * composite components, for example ScrollBar's contain ArrowButtons.
  57. * To bind a style to this <code>Region</code> use the name
  58. * <code>ArrowButton</code>.
  59. */
  60. public static final Region ARROW_BUTTON = new Region("ArrowButton",
  61. "ArrowButtonUI");
  62. /**
  63. * Button region. To bind a style to this <code>Region</code> use the name
  64. * <code>Button</code>.
  65. */
  66. public static final Region BUTTON = new Region("Button",
  67. "ButtonUI");
  68. /**
  69. * CheckBox region. To bind a style to this <code>Region</code> use the name
  70. * <code>CheckBox</code>.
  71. */
  72. public static final Region CHECK_BOX = new Region("CheckBox",
  73. "CheckBoxUI");
  74. /**
  75. * CheckBoxMenuItem region. To bind a style to this <code>Region</code> use
  76. * the name <code>CheckBoxMenuItem</code>.
  77. */
  78. public static final Region CHECK_BOX_MENU_ITEM = new Region(
  79. "CheckBoxMenuItem", "CheckBoxMenuItemUI");
  80. /**
  81. * ColorChooser region. To bind a style to this <code>Region</code> use
  82. * the name <code>ColorChooser</code>.
  83. */
  84. public static final Region COLOR_CHOOSER = new Region(
  85. "ColorChooser", "ColorChooserUI");
  86. /**
  87. * ComboBox region. To bind a style to this <code>Region</code> use
  88. * the name <code>ComboBox</code>.
  89. */
  90. public static final Region COMBO_BOX = new Region(
  91. "ComboBox", "ComboBoxUI");
  92. /**
  93. * DesktopPane region. To bind a style to this <code>Region</code> use
  94. * the name <code>DesktopPane</code>.
  95. */
  96. public static final Region DESKTOP_PANE = new Region("DesktopPane",
  97. "DesktopPaneUI");
  98. /**
  99. * DesktopIcon region. To bind a style to this <code>Region</code> use
  100. * the name <code>DesktopIcon</code>.
  101. */
  102. public static final Region DESKTOP_ICON = new Region("DesktopIcon",
  103. "DesktopIconUI");
  104. /**
  105. * EditorPane region. To bind a style to this <code>Region</code> use
  106. * the name <code>EditorPane</code>.
  107. */
  108. public static final Region EDITOR_PANE = new Region("EditorPane",
  109. "EditorPaneUI");
  110. /**
  111. * FileChooser region. To bind a style to this <code>Region</code> use
  112. * the name <code>FileChooser</code>.
  113. */
  114. public static final Region FILE_CHOOSER = new Region("FileChooser",
  115. "FileChooserUI");
  116. /**
  117. * FormattedTextField region. To bind a style to this <code>Region</code> use
  118. * the name <code>FormattedTextField</code>.
  119. */
  120. public static final Region FORMATTED_TEXT_FIELD = new Region(
  121. "FormattedTextField", "FormattedTextFieldUI");
  122. /**
  123. * InternalFrame region. To bind a style to this <code>Region</code> use
  124. * the name <code>InternalFrame</code>.
  125. */
  126. public static final Region INTERNAL_FRAME = new Region("InternalFrame",
  127. "InternalFrameUI");
  128. /**
  129. * TitlePane of an InternalFrame. The TitlePane typically
  130. * shows a menu, title, widgets to manipulate the internal frame.
  131. * To bind a style to this <code>Region</code> use the name
  132. * <code>InternalFrameTitlePane</code>.
  133. */
  134. public static final Region INTERNAL_FRAME_TITLE_PANE =
  135. new Region("InternalFrameTitlePane",
  136. "InternalFrameTitlePaneUI");
  137. /**
  138. * Label region. To bind a style to this <code>Region</code> use the name
  139. * <code>Label</code>.
  140. */
  141. public static final Region LABEL = new Region("Label", "LabelUI");
  142. /**
  143. * List region. To bind a style to this <code>Region</code> use the name
  144. * <code>List</code>.
  145. */
  146. public static final Region LIST = new Region("List", "ListUI");
  147. /**
  148. * Menu region. To bind a style to this <code>Region</code> use the name
  149. * <code>Menu</code>.
  150. */
  151. public static final Region MENU = new Region("Menu", "MenuUI");
  152. /**
  153. * MenuBar region. To bind a style to this <code>Region</code> use the name
  154. * <code>MenuBar</code>.
  155. */
  156. public static final Region MENU_BAR = new Region("MenuBar", "MenuBarUI");
  157. /**
  158. * MenuItem region. To bind a style to this <code>Region</code> use the name
  159. * <code>MenuItem</code>.
  160. */
  161. public static final Region MENU_ITEM = new Region("MenuItem","MenuItemUI");
  162. /**
  163. * Accelerator region of a MenuItem. To bind a style to this
  164. * <code>Region</code> use the name <code>MenuItemAccelerator</code>.
  165. */
  166. public static final Region MENU_ITEM_ACCELERATOR = new Region(
  167. "MenuItemAccelerator");
  168. /**
  169. * OptionPane region. To bind a style to this <code>Region</code> use
  170. * the name <code>OptionPane</code>.
  171. */
  172. public static final Region OPTION_PANE = new Region("OptionPane",
  173. "OptionPaneUI");
  174. /**
  175. * Panel region. To bind a style to this <code>Region</code> use the name
  176. * <code>Panel</code>.
  177. */
  178. public static final Region PANEL = new Region("Panel", "PanelUI");
  179. /**
  180. * PasswordField region. To bind a style to this <code>Region</code> use
  181. * the name <code>PasswordField</code>.
  182. */
  183. public static final Region PASSWORD_FIELD = new Region("PasswordField",
  184. "PasswordFieldUI");
  185. /**
  186. * PopupMenu region. To bind a style to this <code>Region</code> use
  187. * the name <code>PopupMenu</code>.
  188. */
  189. public static final Region POPUP_MENU = new Region("PopupMenu",
  190. "PopupMenuUI");
  191. /**
  192. * PopupMenuSeparator region. To bind a style to this <code>Region</code>
  193. * use the name <code>PopupMenuSeparator</code>.
  194. */
  195. public static final Region POPUP_MENU_SEPARATOR = new Region(
  196. "PopupMenuSeparator", "PopupMenuSeparatorUI");
  197. /**
  198. * ProgressBar region. To bind a style to this <code>Region</code>
  199. * use the name <code>ProgressBar</code>.
  200. */
  201. public static final Region PROGRESS_BAR = new Region("ProgressBar",
  202. "ProgressBarUI");
  203. /**
  204. * RadioButton region. To bind a style to this <code>Region</code>
  205. * use the name <code>RadioButton</code>.
  206. */
  207. public static final Region RADIO_BUTTON = new Region(
  208. "RadioButton", "RadioButtonUI");
  209. /**
  210. * RegionButtonMenuItem region. To bind a style to this <code>Region</code>
  211. * use the name <code>RadioButtonMenuItem</code>.
  212. */
  213. public static final Region RADIO_BUTTON_MENU_ITEM = new Region(
  214. "RadioButtonMenuItem", "RadioButtonMenuItemUI");
  215. /**
  216. * RootPane region. To bind a style to this <code>Region</code> use
  217. * the name <code>RootPane</code>.
  218. */
  219. public static final Region ROOT_PANE = new Region("RootPane",
  220. "RootPaneUI");
  221. /**
  222. * ScrollBar region. To bind a style to this <code>Region</code> use
  223. * the name <code>ScrollBar</code>.
  224. */
  225. public static final Region SCROLL_BAR = new Region("ScrollBar",
  226. "ScrollBarUI");
  227. /**
  228. * Track of the ScrollBar. To bind a style to this <code>Region</code> use
  229. * the name <code>ScrollBarTrack</code>.
  230. */
  231. public static final Region SCROLL_BAR_TRACK = new Region("ScrollBarTrack");
  232. /**
  233. * Thumb of the ScrollBar. The thumb is the region of the ScrollBar
  234. * that gives a graphical depiction of what percentage of the View is
  235. * currently visible. To bind a style to this <code>Region</code> use
  236. * the name <code>ScrollBarThumb</code>.
  237. */
  238. public static final Region SCROLL_BAR_THUMB = new Region("ScrollBarThumb");
  239. /**
  240. * ScrollPane region. To bind a style to this <code>Region</code> use
  241. * the name <code>ScrollPane</code>.
  242. */
  243. public static final Region SCROLL_PANE = new Region("ScrollPane",
  244. "ScrollPaneUI");
  245. /**
  246. * Separator region. To bind a style to this <code>Region</code> use
  247. * the name <code>Separator</code>.
  248. */
  249. public static final Region SEPARATOR = new Region("Separator",
  250. "SeparatorUI");
  251. /**
  252. * Slider region. To bind a style to this <code>Region</code> use
  253. * the name <code>Slider</code>.
  254. */
  255. public static final Region SLIDER = new Region("Slider", "SliderUI");
  256. /**
  257. * Track of the Slider. To bind a style to this <code>Region</code> use
  258. * the name <code>SliderTrack</code>.
  259. */
  260. public static final Region SLIDER_TRACK = new Region("SliderTrack");
  261. /**
  262. * Thumb of the Slider. The thumb of the Slider identifies the current
  263. * value. To bind a style to this <code>Region</code> use the name
  264. * <code>SliderThumb</code>.
  265. */
  266. public static final Region SLIDER_THUMB = new Region("SliderThumb");
  267. /**
  268. * Spinner region. To bind a style to this <code>Region</code> use the name
  269. * <code>Spinner</code>.
  270. */
  271. public static final Region SPINNER = new Region("Spinner", "SpinnerUI");
  272. /**
  273. * SplitPane region. To bind a style to this <code>Region</code> use the name
  274. * <code>SplitPane</code>.
  275. */
  276. public static final Region SPLIT_PANE = new Region("SplitPane",
  277. "SplitPaneUI");
  278. /**
  279. * Divider of the SplitPane. To bind a style to this <code>Region</code>
  280. * use the name <code>SplitPaneDivider</code>.
  281. */
  282. public static final Region SPLIT_PANE_DIVIDER = new Region(
  283. "SplitPaneDivider");
  284. /**
  285. * TabbedPane region. To bind a style to this <code>Region</code> use
  286. * the name <code>TabbedPane</code>.
  287. */
  288. public static final Region TABBED_PANE = new Region("TabbedPane",
  289. "TabbedPaneUI");
  290. /**
  291. * Region of a TabbedPane for one tab. To bind a style to this
  292. * <code>Region</code> use the name <code>TabbedPaneTab</code>.
  293. */
  294. public static final Region TABBED_PANE_TAB = new Region("TabbedPaneTab");
  295. /**
  296. * Region of a TabbedPane containing the tabs. To bind a style to this
  297. * <code>Region</code> use the name <code>TabbedPaneTabArea</code>.
  298. */
  299. public static final Region TABBED_PANE_TAB_AREA =
  300. new Region("TabbedPaneTabArea");
  301. /**
  302. * Region of a TabbedPane containing the content. To bind a style to this
  303. * <code>Region</code> use the name <code>TabbedPaneContent</code>.
  304. */
  305. public static final Region TABBED_PANE_CONTENT =
  306. new Region("TabbedPaneContent");
  307. /**
  308. * Table region. To bind a style to this <code>Region</code> use
  309. * the name <code>Table</code>.
  310. */
  311. public static final Region TABLE = new Region("Table", "TableUI");
  312. /**
  313. * TableHeader region. To bind a style to this <code>Region</code> use
  314. * the name <code>TableHeader</code>.
  315. */
  316. public static final Region TABLE_HEADER = new Region("TableHeader",
  317. "TableHeaderUI");
  318. /**
  319. * TextArea region. To bind a style to this <code>Region</code> use
  320. * the name <code>TextArea</code>.
  321. */
  322. public static final Region TEXT_AREA = new Region("TextArea",
  323. "TextAreaUI");
  324. /**
  325. * TextField region. To bind a style to this <code>Region</code> use
  326. * the name <code>TextField</code>.
  327. */
  328. public static final Region TEXT_FIELD = new Region("TextField",
  329. "TextFieldUI");
  330. /**
  331. * TextPane region. To bind a style to this <code>Region</code> use
  332. * the name <code>TextPane</code>.
  333. */
  334. public static final Region TEXT_PANE = new Region("TextPane",
  335. "TextPaneUI");
  336. /**
  337. * ToggleButton region. To bind a style to this <code>Region</code> use
  338. * the name <code>ToggleButton</code>.
  339. */
  340. public static final Region TOGGLE_BUTTON = new Region("ToggleButton",
  341. "ToggleButtonUI");
  342. /**
  343. * ToolBar region. To bind a style to this <code>Region</code> use
  344. * the name <code>ToolBar</code>.
  345. */
  346. public static final Region TOOL_BAR = new Region("ToolBar", "ToolBarUI");
  347. /**
  348. * Region of the ToolBar containing the content. To bind a style to this
  349. * <code>Region</code> use the name <code>ToolBarContent</code>.
  350. */
  351. public static final Region TOOL_BAR_CONTENT = new Region("ToolBarContent");
  352. /**
  353. * Region for the Window containing the ToolBar. To bind a style to this
  354. * <code>Region</code> use the name <code>ToolBarDragWindow</code>.
  355. */
  356. public static final Region TOOL_BAR_DRAG_WINDOW = new Region(
  357. "ToolBarDragWindow", null, false);
  358. /**
  359. * ToolTip region. To bind a style to this <code>Region</code> use
  360. * the name <code>ToolTip</code>.
  361. */
  362. public static final Region TOOL_TIP = new Region("ToolTip", "ToolTipUI");
  363. /**
  364. * ToolBar separator region. To bind a style to this <code>Region</code> use
  365. * the name <code>ToolBarSeparator</code>.
  366. */
  367. public static final Region TOOL_BAR_SEPARATOR = new Region(
  368. "ToolBarSeparator", "ToolBarSeparatorUI");
  369. /**
  370. * Tree region. To bind a style to this <code>Region</code> use the name
  371. * <code>Tree</code>.
  372. */
  373. public static final Region TREE = new Region("Tree", "TreeUI");
  374. /**
  375. * Region of the Tree for one cell. To bind a style to this
  376. * <code>Region</code> use the name <code>TreeCell</code>.
  377. */
  378. public static final Region TREE_CELL = new Region("TreeCell");
  379. /**
  380. * Viewport region. To bind a style to this <code>Region</code> use
  381. * the name <code>Viewport</code>.
  382. */
  383. public static final Region VIEWPORT = new Region("Viewport", "ViewportUI");
  384. private String name;
  385. private boolean subregion;
  386. static Region getRegion(JComponent c) {
  387. return (Region)uiToRegionMap.get(c.getUIClassID());
  388. }
  389. static void registerUIs(UIDefaults table) {
  390. Iterator uis = uiToRegionMap.keySet().iterator();
  391. while (uis.hasNext()) {
  392. Object key = uis.next();
  393. table.put(key, "javax.swing.plaf.synth.SynthLookAndFeel");
  394. }
  395. }
  396. Region(String name) {
  397. this(name, null, true);
  398. }
  399. Region(String name, String ui) {
  400. this(name, ui, false);
  401. }
  402. /**
  403. * Creates a Region with the specified name. This should only be
  404. * used if you are creating your own <code>JComponent</code> subclass
  405. * with a custom <code>ComponentUI</code> class.
  406. *
  407. * @param name Name of the region
  408. * @param ui String that will be returned from
  409. * <code>component.getUIClassID</code>. This will be null
  410. * if this is a subregion.
  411. * @param subregion Whether or not this is a subregion.
  412. */
  413. protected Region(String name, String ui, boolean subregion) {
  414. if (name == null) {
  415. throw new NullPointerException("You must specify a non-null name");
  416. }
  417. this.name = name;
  418. if (ui != null) {
  419. uiToRegionMap.put(ui, this);
  420. }
  421. this.subregion = subregion;
  422. }
  423. /**
  424. * Returns true if the Region is a subregion of a Component, otherwise
  425. * false. For example, <code>Region.BUTTON</code> corresponds do a
  426. * <code>Component</code> so that <code>Region.BUTTON.isSubregion()</code>
  427. * returns false.
  428. *
  429. * @return true if the Region is a subregion of a Component.
  430. */
  431. public boolean isSubregion() {
  432. return subregion;
  433. }
  434. /**
  435. * Returns the name of the region.
  436. *
  437. * @return name of the Region.
  438. */
  439. public String getName() {
  440. return name;
  441. }
  442. /**
  443. * Returns the name, in lowercase.
  444. */
  445. String getLowerCaseName() {
  446. synchronized(lowerCaseNameMap) {
  447. String lowerCaseName = (String)lowerCaseNameMap.get(this);
  448. if (lowerCaseName == null) {
  449. lowerCaseName = getName().toLowerCase();
  450. lowerCaseNameMap.put(this, lowerCaseName);
  451. }
  452. return lowerCaseName;
  453. }
  454. }
  455. /**
  456. * Returns the name of the Region.
  457. *
  458. * @return name of the Region.
  459. */
  460. public String toString() {
  461. return name;
  462. }
  463. }