Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
InspectorCellRenderer.java
1 
21 package net.squiz.matrix.inspector;
22 
23 import net.squiz.matrix.core.*;
24 import net.squiz.matrix.matrixtree.*;
25 import javax.swing.*;
26 import javax.swing.JTable;
27 import javax.swing.JLabel;
28 import javax.swing.SwingConstants;
29 import javax.swing.table.TableCellRenderer;
30 import javax.swing.tree.*;
31 import javax.swing.border.LineBorder;
32 
33 import java.awt.*;
34 import java.awt.Component;
35 
45 public class InspectorCellRenderer extends JLabel
46  implements TableCellRenderer,
47  MatrixConstants {
48 
50  private Asset asset;
51 
52  private boolean selected = false;
53 
54  private boolean allSelected = false;
55 
56 
57  //{{{ Public Methods
58 
71  JTable table,
72  Object value,
73  boolean isSelected,
74  boolean hasFocus,
75  int row,
76  int column) {
77 
78  if (value == null)
79  return null;
80 
81  else if (value instanceof MatrixTreeNode) {
82  MatrixTreeNode node = (MatrixTreeNode) value;
83  Asset asset = node.getAsset();
84  this.asset = asset;
85  this.selected = isSelected;
86 
87  String name = node.getName();
88  int len = 12;
89  if (name.length() > len) {
90  name = name.substring(0, len - 3) + "...";
91  }
92  String numAssetsStr = null;
93  int numKids = node.getAsset().getNumKids();
94  if (numKids < 0) {
95  numAssetsStr = "unknown";
96  } else if (numKids == 1) {
97  numAssetsStr = numKids + " asset";
98  } else {
99  numAssetsStr = numKids + " assets";
100  }
101 
102  setText("<html><center>" + name + "<br><font color=\"#AAAAAA\">" + numAssetsStr + "</font></center></html>");
103  String toolTip = new String("<html>" +
104  node.getName() + "<br>" +
105  asset.getType().getName() + " [" + asset.getId() + "]" +
106  "</html>");
107  setToolTipText(toolTip);
108  setFont(PLAIN_FONT_10);
109  setDisabledIcon(null);
110 
111  if (asset.getType() != null) {
112  setIcon(asset.getType().getIcon());
113  }
114  setVerticalTextPosition(JLabel.BOTTOM);
115  setHorizontalTextPosition(JLabel.CENTER);
116  setHorizontalAlignment(JLabel.CENTER);
117  setIconTextGap(1);
118  } else if (value instanceof DefaultMutableTreeNode) {
119  DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
120  if (node.getUserObject() instanceof String) {
121  setText((String) node.getUserObject());
122  }
123  }
124 
125  return this;
126  }
127 
134  public void paint(Graphics g) {
135  Graphics2D g2;
136  int iconSquareOffset = (int) ( getSize().getWidth() - getIcon().getIconWidth() ) / 2;
137  int iconSquareWidth = getIcon().getIconWidth() + 10;
138  int iconSquareHeight = getIcon().getIconHeight() + 10;
139 
140  if (selected || allSelected) {
141  FontMetrics fm = getFontMetrics(getFont());
142 
143  int textWidth = fm.stringWidth(getText());
144  textWidth = textWidth > getWidth() ? getWidth() : textWidth;
145  textWidth = textWidth - 1 < iconSquareWidth ? iconSquareWidth + 1 : textWidth;
146  textWidth = textWidth % 2 != 0 ? textWidth++ : textWidth;
147 
148  int offset = ((getWidth() - textWidth) / 2) + 1;
149 
150  int textOffset = getIcon().getIconHeight() + getIconTextGap() + 3;
151  int height = fm.getHeight() - 2;
152 
153  //g.setColor(new Color(0,0,128));
154 
155  // Highlight text only
156  /*g.setColor(asset.getStatusColour());
157  g.fillRect(offset, textOffset, textWidth - 1, height);
158  g.setColor(asset.getStatusColour().darker());
159  g.drawRect(offset, textOffset, textWidth - 1, height);*/
160 
161  // Highlight text only, but the width of the cell rather than the text
162  g.setColor(asset.getStatusColour());
163  g.fillRect(0, textOffset, getWidth() - 1, height);
164  g.setColor(asset.getStatusColour().darker());
165  g.drawRect(0, textOffset, getWidth() - 1, height);
166 
167  // Highlight entire cell
168  /*g.setColor(asset.getStatusColour());
169  g.fillRect(0, 0, getWidth() - 1, getHeight());
170  g.setColor(asset.getStatusColour().darker());
171  g.drawRect(0, 0, getWidth() - 1, getHeight());*/
172 
173  //g.setColor(asset.getStatusColour().darker());
174 
175 
176  //g2 = (Graphics2D)g;
177  //g2.setStroke(new BasicStroke(2));
178  //g2.drawRect(0,0, getWidth() - 2, getWidth() - 2);
179  //g.drawRect(iconSquareOffset - 5, 1, iconSquareWidth, iconSquareHeight);
180  //g = (Graphics)g2;
181  //setForeground(Color.WHITE);
182  }
183  else {
184  //g2 = (Graphics2D)g;
185  //g2.setStroke(new BasicStroke(1));
186  //g2.setColor(new Color(192,192,192));
187  //g.drawRect(iconSquareOffset - 5, 1, iconSquareWidth, iconSquareHeight);
188  //g2.drawRect(0,0, getWidth() - 2, iconSquareHeight);
189 
190  //g = (Graphics)g2;
191  //setForeground(Color.BLACK);
192  }
193 
194  super.paint(g);
195  }
196 
202  public void flipSelection() {
203  allSelected = (allSelected) ? false : true;
204  }
205 
206  //}}}
207 
208  //{{{ Protected Methods
209  //}}}
210 
211  //{{{ Package Private Methods
212  //}}}
213 
214  //{{{ Private Methods
215  //}}}
216 
217  //{{{ Protected Inner Classes
218  //}}}
219 
220  //{{{ Inner Classes
221  //}}}
222 }