001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.game.editor.common;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import java.awt.Graphics;
047: import java.awt.Graphics2D;
048: import java.awt.event.MouseEvent;
049: import javax.swing.JComponent;
050: import javax.swing.JList;
051: import javax.swing.ListCellRenderer;
052: import org.netbeans.modules.vmd.game.model.StaticTile;
053: import org.netbeans.modules.vmd.game.view.ColorConstants;
054: import org.openide.util.NbBundle;
055:
056: public class TileCellRenderer extends JComponent implements
057: ListCellRenderer {
058:
059: private static final int MIN_TILE_SIZE = 30;
060:
061: private int padX;
062: private int padY;
063:
064: //TODO : replace those colors with the correct color resources from L&F
065: private final static Color MOST_COLOR = ColorConstants.COLOR_OUTLINE_SELECTED;
066: private final static Color NO_COLOR = ColorConstants.COLOR_OUTLINE_PLAIN;
067: private final static Color MEDIUM_COLOR = ColorConstants.COLOR_OUTLINE_HILITE;
068:
069: private StaticTile tile;
070: private boolean isSelected;
071: private boolean hasFocus;
072:
073: public TileCellRenderer(int padX, int padY) {
074: this .padX = padX;
075: this .padY = padY;
076: }
077:
078: public String getToolTipText(MouseEvent e) {
079: return NbBundle.getMessage(TileCellRenderer.class,
080: "TileCellRenderer.tooltip", this .tile.getIndex());
081: }
082:
083: public void paintComponent(Graphics g) {
084: Color c = this .isSelected && this .hasFocus ? MOST_COLOR
085: : (this .isSelected ? MEDIUM_COLOR : NO_COLOR);
086: g.setColor(c);
087: g.fillRect(0, 0, padX, this .getHeight()); //left vertical
088: g.fillRect(this .getWidth() - padX, 0, padX, this .getHeight()); //right vertical
089: g.fillRect(padX, 0, this .getWidth() - 2 * padX, padY); //top horizontal
090: g.fillRect(padX, this .getHeight() - padY, this .getWidth() - 2
091: * padX, padY); //bottom horizontal
092: if (this .tile != null) {
093: this .tile.paint((Graphics2D) g, padX, padY, this .getWidth()
094: - 2 * padX, this .getHeight() - 2 * padY);
095: }
096: g.setColor(Color.WHITE);
097: g.drawRect(0, 0, this .getWidth() - 1, this .getHeight() - 1);
098: this .paintBorder(g);
099: }
100:
101: public Component getListCellRendererComponent(JList list,
102: Object value, int index, boolean isSelected,
103: boolean hasFocus) {
104: if (value instanceof StaticTile) {
105: StaticTile tile = (StaticTile) value;
106: this .tile = tile;
107: this .isSelected = isSelected;
108: this .hasFocus = hasFocus;
109:
110: int tileW = tile.getWidth();
111: int tileH = tile.getHeight();
112:
113: int width = tileW;
114: int height = tileH;
115:
116: if (tileW < tileH && tileW < MIN_TILE_SIZE) {
117: float ratio = (float) MIN_TILE_SIZE / (float) tileW;
118: width = MIN_TILE_SIZE;
119: height *= ratio;
120: } else if (tileH < tileW && tileH < MIN_TILE_SIZE) {
121: float ratio = (float) MIN_TILE_SIZE / (float) tileH;
122: height = MIN_TILE_SIZE;
123: width *= ratio;
124: } else if (tileH == tileW && tileH < MIN_TILE_SIZE) {
125: width = MIN_TILE_SIZE;
126: height = MIN_TILE_SIZE;
127: }
128:
129: Dimension dimension = new Dimension(width + 2 * padX,
130: height + 2 * padY);
131: this .setPreferredSize(dimension);
132: return this ;
133: }
134: throw new IllegalArgumentException(
135: "Only org.netbeans.mobility.game.model.Tile or java.lang.Integer can be rendered."); // NOI18N
136: }
137: }
|