001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.demo;
022:
023: import java.awt.BorderLayout;
024: import java.awt.Dimension;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.Insets;
028: import java.awt.Rectangle;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.event.ItemEvent;
032: import java.awt.event.ItemListener;
033: import com.javujavu.javux.wings.Style;
034: import com.javujavu.javux.wings.WingCombo;
035: import com.javujavu.javux.wings.WingImage;
036: import com.javujavu.javux.wings.WingLabel;
037: import com.javujavu.javux.wings.WingPanel;
038: import com.javujavu.javux.wings.WingScrollPane;
039: import com.javujavu.javux.wings.WingSkin;
040: import com.javujavu.javux.wings.WingSpinner;
041: import com.javujavu.javux.wings.WingSplitPane;
042: import com.javujavu.javux.wings.item.LabelItem;
043:
044: public class ImagePanel extends WingPanel implements ActionListener,
045: ItemListener {
046: public class DummyLabel {
047: public String toString() {
048: if (lImage == null)
049: return null;
050: Dimension size = lImage.getSize();
051: return "Stretched Image " + size.width + "x" + size.height
052: + "px";
053: }
054: }
055:
056: protected WingImage image;
057: private WingLabel lImage;
058: private WingSpinner spLeft;
059: private WingSpinner spTop;
060: private WingSpinner spRight;
061: private WingSpinner spBottom;
062: private WingCombo cbTile;
063: private WingCombo cbSource;
064: private WingSpinner spX;
065: private WingSpinner spY;
066: private WingSpinner spPart;
067: private WingSpinner spWidth;
068: private WingSpinner spHeight;
069: private WingLabel lSrcInfo;
070: private WingLabel lSrc;
071: private WingLabel lOrigInfo;
072: private WingLabel lOrig;
073: private WingCombo cbImages;
074: private WingSpinner spAlpha;
075:
076: public ImagePanel(WingSetPanel owner) {
077: this .setLayout(new BorderLayout());
078: WingPanel panel1 = new WingPanel(new GridBagLayout());
079:
080: WingSplitPane sp;
081: this .add(sp = new WingSplitPane(HORIZONTAL),
082: BorderLayout.CENTER);
083: sp.setLeftComponent(new WingScrollPane(panel1, false));
084: sp.setRightComponent(sp = new WingSplitPane(VERTICAL));
085: sp.setBottomComponent(lImage = new WingLabel(new DummyLabel(),
086: CENTER));
087: WingPanel panel3;
088: sp.setTopComponent(panel3 = new WingPanel(new BorderLayout()));
089: WingSplitPane sp2;
090: panel3.add(sp2 = new WingSplitPane(HORIZONTAL),
091: BorderLayout.CENTER);
092: sp2.setResizeWeight(0.5);
093: WingPanel panel0;
094: sp2.setLeftComponent(new WingScrollPane(panel0 = new WingPanel(
095: new GridBagLayout()), false));
096: GridBagConstraints c0 = new GridBagConstraints();
097: c0.gridwidth = GridBagConstraints.REMAINDER;
098: panel0.add(lOrigInfo = new WingLabel(null, WingLabel.CENTER),
099: c0);
100: panel0.add(lOrig = new WingLabel(null, WingLabel.CENTER), c0);
101: sp2.setRightComponent(new WingScrollPane(
102: panel0 = new WingPanel(new GridBagLayout()), false));
103: panel0
104: .add(lSrcInfo = new WingLabel(null, WingLabel.CENTER),
105: c0);
106: panel0.add(lSrc = new WingLabel(null, WingLabel.CENTER), c0);
107: WingPanel panel2;
108: panel3.add(panel2 = new WingPanel(new GridBagLayout()),
109: BorderLayout.NORTH);
110: panel2.add(new WingLabel("Source Image: "));
111: panel2.add(cbImages = new WingCombo());
112: cbImages.setTooltip(new LabelItem(
113: "choose image from loaded skin style sheet",
114: WingSet.imgEye));
115:
116: GridBagConstraints c1 = new GridBagConstraints();
117: c1.weighty = 1.0;
118: c1.gridwidth = GridBagConstraints.REMAINDER;
119: c1.fill = GridBagConstraints.HORIZONTAL;
120: c1.insets = new Insets(1, 4, 1, 4);
121:
122: panel1.add(new WingLabel("WingImage parameters"), c1);
123: panel2 = new WingPanel(new GridBagLayout());
124: panel2.setStyleId("border1");
125: c1.insets = new Insets(1, 4, 8, 4);
126: panel1.add(panel2, c1);
127:
128: GridBagConstraints c2 = new GridBagConstraints();
129: c2.insets = new Insets(1, 5, 1, 5);
130: c2.weighty = 1.0;
131: c2.fill = GridBagConstraints.HORIZONTAL;
132:
133: c2.gridwidth = GridBagConstraints.REMAINDER;
134: panel2.add(new WingLabel("stretch margin"), c2);
135:
136: c2.insets = new Insets(1, 5, 0, 5);
137:
138: c2.gridwidth = 1;
139: c2.anchor = GridBagConstraints.EAST;
140: panel2.add(new WingLabel("left"), c2);
141: c2.anchor = GridBagConstraints.WEST;
142: c2.gridwidth = GridBagConstraints.REMAINDER;
143: panel2.add(spLeft = new WingSpinner(), c2);
144:
145: c2.gridwidth = 1;
146: c2.anchor = GridBagConstraints.EAST;
147: panel2.add(new WingLabel("top"), c2);
148: c2.anchor = GridBagConstraints.WEST;
149: c2.gridwidth = GridBagConstraints.REMAINDER;
150: panel2.add(spTop = new WingSpinner(), c2);
151:
152: c2.gridwidth = 1;
153: c2.anchor = GridBagConstraints.EAST;
154: panel2.add(new WingLabel("right"), c2);
155: c2.anchor = GridBagConstraints.WEST;
156: c2.gridwidth = GridBagConstraints.REMAINDER;
157: panel2.add(spRight = new WingSpinner(), c2);
158:
159: c2.gridwidth = 1;
160: c2.anchor = GridBagConstraints.EAST;
161: panel2.add(new WingLabel("bottom"), c2);
162: c2.anchor = GridBagConstraints.WEST;
163: c2.gridwidth = GridBagConstraints.REMAINDER;
164: panel2.add(spBottom = new WingSpinner(), c2);
165:
166: c2.insets = new Insets(1, 5, 0, 5);
167: panel2.add(new WingLabel("stretch tile"), c2);
168: c2.insets = new Insets(1, 5, 5, 5);
169: panel2.add(cbTile = new WingCombo(), c2);
170: cbTile.addItem("none");
171: cbTile.addItem("NorthWest");
172: cbTile.addItem("North");
173: cbTile.addItem("NorthEast");
174: cbTile.addItem("East");
175: cbTile.addItem("SouthEast");
176: cbTile.addItem("South");
177: cbTile.addItem("SouthWest");
178: cbTile.addItem("West");
179:
180: panel2 = new WingPanel(new GridBagLayout());
181: panel2.setStyleId("border1");
182: c1.insets = new Insets(1, 4, 5, 4);
183: panel1.add(panel2, c1);
184:
185: c2.insets = new Insets(1, 5, 1, 5);
186: panel2.add(new WingLabel("source area", WingLabel.CENTER), c2);
187: c2.insets = new Insets(1, 5, 0, 5);
188: panel2.add(cbSource = new WingCombo(), c2);
189: cbSource.addItem("whole image");
190: cbSource.addItem("rectangle");
191: cbSource.addItem("part");
192:
193: c2.gridwidth = 1;
194: c2.anchor = GridBagConstraints.EAST;
195: panel2.add(new WingLabel("part no"), c2);
196: c2.anchor = GridBagConstraints.WEST;
197: c2.gridwidth = GridBagConstraints.REMAINDER;
198: panel2.add(spPart = new WingSpinner(), c2);
199:
200: c2.gridwidth = 1;
201: c2.anchor = GridBagConstraints.EAST;
202: panel2.add(new WingLabel("x"), c2);
203: c2.anchor = GridBagConstraints.WEST;
204: c2.gridwidth = GridBagConstraints.REMAINDER;
205: panel2.add(spX = new WingSpinner(), c2);
206:
207: c2.gridwidth = 1;
208: c2.anchor = GridBagConstraints.EAST;
209: panel2.add(new WingLabel("y"), c2);
210: c2.anchor = GridBagConstraints.WEST;
211: c2.gridwidth = GridBagConstraints.REMAINDER;
212: panel2.add(spY = new WingSpinner(), c2);
213:
214: c2.gridwidth = 1;
215: c2.anchor = GridBagConstraints.EAST;
216: panel2.add(new WingLabel("width"), c2);
217: c2.anchor = GridBagConstraints.WEST;
218: c2.gridwidth = GridBagConstraints.REMAINDER;
219: panel2.add(spWidth = new WingSpinner(), c2);
220:
221: c2.insets = new Insets(1, 5, 5, 5);
222: c2.gridwidth = 1;
223: c2.anchor = GridBagConstraints.EAST;
224: panel2.add(new WingLabel("height"), c2);
225: c2.anchor = GridBagConstraints.WEST;
226: c2.gridwidth = GridBagConstraints.REMAINDER;
227: panel2.add(spHeight = new WingSpinner(), c2);
228:
229: panel2 = new WingPanel(new GridBagLayout());
230: panel2.setStyleId("border1");
231: c2.insets = new Insets(5, 5, 5, 5);
232: panel1.add(panel2, c1);
233: c2.gridwidth = 1;
234: c2.anchor = GridBagConstraints.EAST;
235: panel2.add(new WingLabel("alpha"), c2);
236: c2.anchor = GridBagConstraints.WEST;
237: c2.gridwidth = GridBagConstraints.REMAINDER;
238: panel2.add(spAlpha = new WingSpinner(), c2);
239: spAlpha.setRange(0, 255);
240:
241: spPart.setValue(9);
242: spX.setValue(30);
243: spY.setValue(0);
244: spWidth.setValue(40);
245: spHeight.setValue(20);
246:
247: refillSourceImages();
248:
249: this .addActionListener(this );
250: this .addItemListener(this );
251: }
252:
253: public void loadSkin() {
254: super .loadSkin();
255: if (cbSource != null)
256: refillSourceImages();
257: }
258:
259: private void refillSourceImages() {
260: int sel = cbImages.getSelectedIndex();
261: cbImages.removeAllItems();
262: String[] ids = { "aztec3.panel.normal", "aztec.panel.normal",
263: "aztec2.panel.normal", "aztec4.panel.normal",
264: "aztec5.panel.normal", "button.normal", "button.hover",
265: "custom_image.button.normal", "button.on.normal",
266: "window.normal", "border1.panel.normal",
267: "border3.panel.normal", "border2.panel.normal",
268: "tab.normal", "tab.selected", "scroll.left.normal",
269: "scroll.hthumb.normal", "scroll.right.pressed",
270: "radio.normal.icon", "checkbox.on.normal.icon",
271: "image not found indicator" };
272: for (int i = 0; i < ids.length; i++) {
273: cbImages.addItem(ids[i]);
274: }
275: String[] ii = WingSkin.listImages();
276: for (int i = 0; i < ii.length; i++) {
277: cbImages.addItem(ii[i]);
278: }
279: if (sel >= cbImages.getItemCount() || sel < 0)
280: sel = 0;
281: cbImages.setSelectedIndex(sel);
282: setImage(cbImages);
283: }
284:
285: private void setImage(Object src) {
286: if (src == cbImages) {
287: WingImage img = WingSkin.getImage((String) cbImages
288: .getSelectedItem(), null);
289: cbTile.setSelectedIndex(img.getTile());
290: Rectangle r = img.getSourceRect();
291: if (r.x == 0 && r.y == 0
292: && r.width == img.getImage().getWidth(null)
293: && r.height == img.getImage().getHeight(null)) {
294: cbSource.setSelectedIndex(0);
295: } else {
296: cbSource.setSelectedIndex(1);
297: spX.setValue(r.x);
298: spY.setValue(r.y);
299: spWidth.setValue(r.width);
300: spHeight.setValue(r.height);
301: }
302: Insets stretch = img.getStretch();
303: spLeft.setValue((stretch != null) ? stretch.left : 0);
304: spRight.setValue((stretch != null) ? stretch.right : 0);
305: spTop.setValue((stretch != null) ? stretch.top : 0);
306: spBottom.setValue((stretch != null) ? stretch.bottom : 0);
307:
308: cbTile.setSelectedIndex(img.getTile());
309: spAlpha.setValue(img.getAlpha());
310:
311: lOrig.setLabel(image = new WingImage(img.getImage()));
312: lOrigInfo.setLabel("Original Image "
313: + image.getImage().getWidth(null) + "x"
314: + image.getImage().getHeight(null) + "px");
315: }
316:
317: int left, top, right, bottom, tile;
318: boolean rect, parts = false;
319: int part, x, y, width, height;
320:
321: int limw = image.getWidth(), limh = image.getHeight();
322:
323: left = spLeft.getValue();
324: top = spTop.getValue();
325: right = spRight.getValue();
326: bottom = spBottom.getValue();
327: part = spPart.getValue();
328: x = spX.getValue();
329: y = spY.getValue();
330: width = spWidth.getValue();
331: height = spHeight.getValue();
332:
333: rect = cbSource.getSelectedIndex() == 1;
334: parts = cbSource.getSelectedIndex() == 2;
335:
336: if (parts) {
337: if (width > limw)
338: width = limw;
339: if (height > limh)
340: height = limh;
341: if (width < 1)
342: width = 1;
343: if (height < 1)
344: height = 1;
345: int pp = (limw / width) * (limh / height) - 1;
346: if (part > pp)
347: part = pp;
348: if (part < 0)
349: part = 0;
350: if (src == cbSource) {
351: spWidth.setRange(1, limw);
352: spHeight.setRange(1, limh);
353: spWidth.setValue(width);
354: spHeight.setValue(height);
355: }
356: if (src == spHeight || src == spWidth || src == cbSource) {
357: spPart.setRange(0, pp);
358: spPart.setValue(part);
359: }
360: } else if (rect) {
361: if (x >= limw)
362: x = limw - 1;
363: if (y >= limh)
364: y = limh - 1;
365: if (x + width > limw)
366: width = limw - x;
367: if (y + height > limh)
368: height = limh - y;
369: if (src == cbSource || src == cbImages) {
370: spX.setRange(0, limw - 1);
371: spY.setRange(0, limh - 1);
372: spX.setValue(x);
373: spY.setValue(y);
374: }
375: if (src == spX || src == cbSource || src == cbImages) {
376: spWidth.setRange(1, limw - x);
377: spWidth.setValue(width);
378: } else if (src == spY || src == cbSource || src == cbImages) {
379: spHeight.setRange(1, limh - y);
380: spHeight.setValue(height);
381: }
382: } else {
383: width = limw;
384: height = limh;
385: }
386: boolean chSize = src == cbSource || src == cbImages
387: || src == spWidth || src == spHeight || src == spX
388: || src == spY;
389: if (src == cbSource) {
390: if (left > width / 4)
391: left = width / 4;
392: if (right > width / 4)
393: right = width / 4;
394: if (top > height / 4)
395: top = height / 4;
396: if (bottom > height / 4)
397: bottom = height / 4;
398: }
399: if (chSize) {
400: if (left >= width)
401: left = width - 1;
402: spLeft.setRange(0, width - 1);
403: spLeft.setValue(left);
404: }
405: if (src == spLeft || chSize) {
406: if (left + right >= width) {
407: right = width - left - 1;
408: }
409: spRight.setRange(0, width - left - 1);
410: spRight.setValue(right);
411: }
412: if (chSize) {
413: if (top >= height)
414: top = height - 1;
415: spTop.setRange(0, height - 1);
416: spTop.setValue(top);
417: }
418: if (src == spTop || chSize) {
419: if (top + bottom >= height) {
420: bottom = height - top - 1;
421: }
422: spBottom.setRange(0, height - top - 1);
423: spBottom.setValue(bottom);
424: }
425:
426: tile = cbTile.getSelectedIndex();
427: // tile= tile==0 ? WingImage.NO_TILE
428: // : tile==1 ? WingImage.TILE_NW
429: // : tile==2 ? WingImage.TILE_N
430: // : tile==3 ? WingImage.TILE_NE
431: // : tile==4 ? WingImage.TILE_E
432: // : tile==5 ? WingImage.TILE_SE
433: // : tile==6 ? WingImage.TILE_S
434: // : tile==7 ? WingImage.TILE_SW
435: // : tile==8 ? WingImage.TILE_W : tile;
436:
437: WingImage i2 = (parts) ? new WingImage(image, part, width,
438: height) : (rect) ? new WingImage(image.getImage(), x,
439: y, width, height) : new WingImage(image.getImage());
440: WingImage i3 = (parts) ? new WingImage(image, part, width,
441: height) : (rect) ? new WingImage(image.getImage(), x,
442: y, width, height) : new WingImage(image.getImage());
443: lSrc.setLabel(i3);
444: lSrcInfo.setLabel("Source Area " + width + "x" + height + "px");
445: i2.setStretch(new Insets(top, left, bottom, right));
446: i2.setTile(tile);
447: i2.setAlpha(spAlpha.getValue());
448: Style s = new Style();
449: s.state = -1;
450: s.image = i2;
451: lImage.setTopStyle(s);
452:
453: spPart.setEnabled(parts);
454: spX.setEnabled(rect);
455: spY.setEnabled(rect);
456: spWidth.setEnabled(rect || parts);
457: spHeight.setEnabled(rect || parts);
458: }
459:
460: public void actionPerformed(ActionEvent e) {
461: setImage(e.getSource());
462: }
463:
464: public void itemStateChanged(ItemEvent e) {
465: /*if(e.getSource()==cbImages)*/setImage(e.getSource());
466: }
467: }
|