001: /*
002: * Copyright 2005 Patrick Gotthardt
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.pagosoft.plaf;
017:
018: import javax.swing.*;
019: import javax.swing.plaf.*;
020: import java.awt.*;
021: import java.io.Serializable;
022:
023: public class PgsIconFactory {
024: private static Icon emptyIcon;
025:
026: public static Icon getEmptyIcon() {
027: if (emptyIcon == null) {
028: emptyIcon = new EmptyIcon(PlafOptions
029: .getDefaultMenuItemIconSize());
030: }
031: return emptyIcon;
032: }
033:
034: private static class EmptyIcon implements Icon, UIResource,
035: Serializable {
036: private Dimension size;
037:
038: public EmptyIcon(Dimension dim) {
039: size = dim;
040: }
041:
042: public void paintIcon(Component c, Graphics g, int x, int y) {
043: return;
044: }
045:
046: public int getIconWidth() {
047: return size.width;
048: }
049:
050: public int getIconHeight() {
051: return size.height;
052: }
053: }
054:
055: protected static void drawCheck(Component c, Graphics g, int x,
056: int y) {
057: g.translate(x, y);
058: g.drawLine(3, 5, 3, 5);
059: g.fillRect(3, 6, 2, 2);
060: g.drawLine(4, 8, 9, 3);
061: g.drawLine(5, 8, 9, 4);
062: g.drawLine(5, 9, 9, 5);
063: g.translate(-x, -y);
064: }
065:
066: private static Icon checkBoxIcon;
067:
068: public static Icon getCheckBoxIcon() {
069: if (checkBoxIcon == null) {
070: checkBoxIcon = new CheckBoxIcon();
071: }
072: return checkBoxIcon;
073: }
074:
075: private static class CheckBoxIcon implements Icon, UIResource,
076: Serializable {
077: protected int getControlSize() {
078: return 13;
079: }
080:
081: public void paintIcon(Component c, Graphics g, int x, int y) {
082: JCheckBox cb = (JCheckBox) c;
083: ButtonModel model = cb.getModel();
084: int controlSize = getControlSize();
085:
086: boolean drawCheck = model.isSelected();
087:
088: if (model.isEnabled()) {
089: // draw background
090: g.setColor(PgsLookAndFeel.getControlHighlight());
091: g.fillRect(x, y, controlSize, controlSize);
092: if (model.isRollover()) {
093: g.setColor(PgsLookAndFeel.getPrimaryControl());
094: g.drawRect(x + 1, y + 1, controlSize - 3,
095: controlSize - 3);
096: }
097: g.setColor(PgsLookAndFeel.getControlDarkShadow());
098: g.drawRect(x, y, controlSize - 1, controlSize - 1);
099: if (model.isPressed() && model.isArmed()) {
100: g.setColor(PgsLookAndFeel.getControlShadow());
101: g.fillRect(x + 1, y + 1, controlSize - 2,
102: controlSize - 2);
103: }
104: g.setColor(PgsLookAndFeel.getControlInfo());
105: } else {
106: g.setColor(PgsLookAndFeel.getControlShadow());
107: g.drawRect(x, y, controlSize - 1, controlSize - 1);
108: }
109:
110: if (drawCheck) {
111: //x++;
112: g.setColor(model.isEnabled() ? PgsLookAndFeel
113: .getPrimaryControlDarkShadow() : PgsLookAndFeel
114: .getControlShadow());
115: drawCheck(c, g, x, y);
116: }
117: }
118:
119: public int getIconWidth() {
120: return getControlSize();
121: }
122:
123: public int getIconHeight() {
124: return getControlSize();
125: }
126: }
127:
128: private static Icon checkBoxMenuItemIcon;
129:
130: public static Icon getCheckBoxMenuItemIcon() {
131: if (checkBoxMenuItemIcon == null) {
132: checkBoxMenuItemIcon = new CheckBoxMenuItemIcon();
133: }
134: return checkBoxMenuItemIcon;
135: }
136:
137: /**
138: * @todo Redo this one... it is ABSOLUTLY the same as CheckBoxIcon...
139: */
140: private static class CheckBoxMenuItemIcon implements Icon,
141: UIResource, Serializable {
142: protected int getControlSize() {
143: return 13;
144: }
145:
146: public void paintIcon(Component c, Graphics g, int x, int y) {
147: JMenuItem cb = (JMenuItem) c;
148: ButtonModel model = cb.getModel();
149: int controlSize = getControlSize();
150:
151: boolean drawCheck = model.isSelected();
152:
153: if (model.isEnabled()) {
154: // draw background
155: g.setColor(PgsLookAndFeel.getControlHighlight());
156: g.fillRect(x, y, controlSize, controlSize);
157: if (model.isRollover()) {
158: g.setColor(PgsLookAndFeel.getPrimaryControl());
159: g.drawRect(x + 1, y + 1, controlSize - 3,
160: controlSize - 3);
161: }
162: g.setColor(PgsLookAndFeel.getControlDarkShadow());
163: g.drawRect(x, y, controlSize - 1, controlSize - 1);
164: if (model.isPressed() && model.isArmed()) {
165: g.setColor(PgsLookAndFeel.getControlShadow());
166: g.fillRect(x + 1, y + 1, controlSize - 2,
167: controlSize - 2);
168: }
169: g.setColor(PgsLookAndFeel.getControlInfo());
170: } else {
171: g.setColor(PgsLookAndFeel.getControlShadow());
172: g.drawRect(x, y, controlSize - 1, controlSize - 1);
173: }
174:
175: if (drawCheck) {
176: g.setColor(model.isEnabled() ? PgsLookAndFeel
177: .getPrimaryControlDarkShadow() : PgsLookAndFeel
178: .getControlShadow());
179: drawCheck(c, g, x, y);
180: }
181: }
182:
183: public int getIconWidth() {
184: return getControlSize();
185: }
186:
187: public int getIconHeight() {
188: return getControlSize();
189: }
190: }
191:
192: private static Icon radioButtonIcon;
193:
194: public static Icon getRadioButtonIcon() {
195: if (radioButtonIcon == null) {
196: radioButtonIcon = new RadioButtonIcon();
197: }
198: return radioButtonIcon;
199: }
200:
201: private static Icon radioMenuItemIcon;
202:
203: public static Icon getRadioButtonMenuItemIcon() {
204: if (radioMenuItemIcon == null) {
205: radioMenuItemIcon = new RadioButtonIcon();
206: }
207: return radioMenuItemIcon;
208: }
209:
210: private static class RadioButtonIcon implements Icon, UIResource,
211: Serializable {
212: protected int getControlSize() {
213: return 13;
214: }
215:
216: public void paintIcon(Component c, Graphics g, int x, int y) {
217: AbstractButton cb = (AbstractButton) c;
218: ButtonModel model = cb.getModel();
219: int controlSize = getControlSize();
220:
221: boolean drawDot = model.isSelected();
222:
223: Graphics2D gfx = (Graphics2D) g;
224: gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
225: RenderingHints.VALUE_ANTIALIAS_ON);
226: if (model.isEnabled()) {
227: // draw background
228: gfx.setColor(PgsLookAndFeel.getControlHighlight());
229: gfx.fillOval(x, y, controlSize, controlSize);
230: if (model.isRollover()) {
231: gfx.setColor(PgsLookAndFeel.getPrimaryControl());
232: gfx.drawOval(x + 1, y + 1, controlSize - 3,
233: controlSize - 3);
234: }
235: gfx.setColor(PgsLookAndFeel.getControlDarkShadow());
236: gfx.drawOval(x, y, controlSize - 1, controlSize - 1);
237: if (model.isPressed() && model.isArmed()) {
238: gfx.setColor(PgsLookAndFeel.getControlShadow());
239: gfx.fillOval(x + 1, y + 1, controlSize - 2,
240: controlSize - 2);
241: }
242: gfx.setColor(PgsLookAndFeel.getControlInfo());
243: } else {
244: gfx.setColor(PgsLookAndFeel.getControlShadow());
245: gfx.drawOval(x, y, controlSize - 1, controlSize - 1);
246: }
247:
248: if (drawDot) {
249: gfx.setColor(model.isEnabled() ? PgsLookAndFeel
250: .getPrimaryControlDarkShadow() : PgsLookAndFeel
251: .getControlShadow());
252: gfx.fillOval(x + 3, y + 3, controlSize - 6,
253: controlSize - 6);
254: }
255: gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
256: RenderingHints.VALUE_ANTIALIAS_OFF);
257: }
258:
259: public int getIconWidth() {
260: return getControlSize();
261: }
262:
263: public int getIconHeight() {
264: return getControlSize();
265: }
266: }
267: }
|