| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package tsukuba_bunko.peko.canvas.select; |
| 20 |
|
|
| 21 |
|
import java.awt.AlphaComposite; |
| 22 |
|
import java.awt.Color; |
| 23 |
|
import java.awt.Dimension; |
| 24 |
|
import java.awt.Font; |
| 25 |
|
import java.awt.Graphics; |
| 26 |
|
import java.awt.Graphics2D; |
| 27 |
|
import java.awt.RenderingHints; |
| 28 |
|
|
| 29 |
|
import java.awt.event.MouseEvent; |
| 30 |
|
import java.awt.event.MouseListener; |
| 31 |
|
|
| 32 |
|
import java.awt.font.LineBreakMeasurer; |
| 33 |
|
import java.awt.font.TextAttribute; |
| 34 |
|
|
| 35 |
|
import java.text.AttributedString; |
| 36 |
|
|
| 37 |
|
import java.util.List; |
| 38 |
|
import java.util.Map; |
| 39 |
|
|
| 40 |
|
import javax.swing.BorderFactory; |
| 41 |
|
import javax.swing.JComponent; |
| 42 |
|
|
| 43 |
|
import tsukuba_bunko.peko.Logger; |
| 44 |
|
import tsukuba_bunko.peko.PekoSystem; |
| 45 |
|
|
| 46 |
|
import tsukuba_bunko.peko.canvas.text.Line; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
public class SelectItemButton extends JComponent implements MouseListener { |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
private static final long serialVersionUID = -3568148242179176104L; |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
public static final String STYLE_WIDTH = "select.button.witdth"; |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
public static final String STYLE_BACKGROUND_COLOR = "select.button.background.color"; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
public static final String STYLE_BACKGROUND_TRANSPARENCY = "select.button.background.transparency"; |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
public static final String STYLE_FOREGROUND_SELECTED = "select.button.foreground.selected"; |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
public static final String STYLE_FOREGROUND_UNSELECTED = "select.button.foreground.unselected"; |
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
public static final String STYLE_FOREGROUND_SHADOW = "select.button.foreground.shadow"; |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
public static final String STYLE_FONT = "select.button.font"; |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
0 |
private SelectCanvas _owner = null; |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
0 |
private SelectItem _item = null; |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
0 |
private List _lines = null; |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
0 |
private Color _foregroundActive = null; |
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
0 |
private Color _foregroundNonActive = null; |
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
0 |
private Color _shadow = null; |
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
0 |
private AlphaComposite _alphaComposite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.5f ); |
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
0 |
private boolean _selected = false; |
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
0 |
private Dimension _size = new Dimension(); |
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
public SelectItemButton( SelectCanvas owner ) |
| 148 |
|
{ |
| 149 |
0 |
super(); |
| 150 |
0 |
setBorder( BorderFactory.createLoweredBevelBorder() ); |
| 151 |
0 |
addMouseListener( this ); |
| 152 |
0 |
_owner = owner; |
| 153 |
0 |
} |
| 154 |
|
|
| 155 |
|
|
| 156 |
|
public void addNotify() |
| 157 |
|
{ |
| 158 |
0 |
super.addNotify(); |
| 159 |
0 |
synchronized( this ) { |
| 160 |
0 |
notify(); |
| 161 |
0 |
} |
| 162 |
0 |
} |
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
public void setSelectItem( SelectItem item ) |
| 169 |
|
{ |
| 170 |
0 |
_item = item; |
| 171 |
0 |
} |
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
public SelectItem getSelectItem() |
| 178 |
|
{ |
| 179 |
0 |
return _item; |
| 180 |
|
} |
| 181 |
|
|
| 182 |
|
|
| 183 |
|
public void prepare( Map style ) |
| 184 |
|
{ |
| 185 |
0 |
Color colorValue = (Color)style.get( SelectItemButton.STYLE_FOREGROUND_SELECTED ); |
| 186 |
0 |
if( colorValue != null ) { |
| 187 |
0 |
_foregroundActive = colorValue; |
| 188 |
0 |
} |
| 189 |
|
else { |
| 190 |
0 |
_foregroundActive = Color.white; |
| 191 |
|
} |
| 192 |
|
|
| 193 |
0 |
colorValue = (Color)style.get( SelectItemButton.STYLE_FOREGROUND_UNSELECTED ); |
| 194 |
0 |
if( colorValue != null ) { |
| 195 |
0 |
_foregroundNonActive = colorValue; |
| 196 |
0 |
} |
| 197 |
|
else { |
| 198 |
0 |
_foregroundNonActive = Color.darkGray; |
| 199 |
|
} |
| 200 |
|
|
| 201 |
0 |
colorValue = (Color)style.get( SelectItemButton.STYLE_BACKGROUND_COLOR ); |
| 202 |
0 |
if( colorValue != null ) { |
| 203 |
0 |
setBackground( colorValue ); |
| 204 |
0 |
} |
| 205 |
|
else { |
| 206 |
0 |
setBackground( Color.black ); |
| 207 |
|
} |
| 208 |
|
|
| 209 |
0 |
colorValue = (Color)style.get( SelectItemButton.STYLE_FOREGROUND_SHADOW ); |
| 210 |
0 |
if( colorValue != null ) { |
| 211 |
0 |
_shadow = colorValue; |
| 212 |
0 |
} |
| 213 |
|
else { |
| 214 |
0 |
_shadow = Color.black; |
| 215 |
|
} |
| 216 |
|
|
| 217 |
0 |
setForeground( _foregroundNonActive ); |
| 218 |
|
|
| 219 |
0 |
Float fv = (Float)style.get( SelectItemButton.STYLE_BACKGROUND_TRANSPARENCY ); |
| 220 |
0 |
if( fv != null ) { |
| 221 |
0 |
if( fv.floatValue() != _alphaComposite.getAlpha() ) { |
| 222 |
0 |
_alphaComposite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, fv.floatValue() ); |
| 223 |
0 |
} |
| 224 |
|
} |
| 225 |
|
else { |
| 226 |
0 |
if( _alphaComposite.getAlpha() != 0.5f ) { |
| 227 |
0 |
_alphaComposite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.5f ); |
| 228 |
|
} |
| 229 |
|
} |
| 230 |
|
|
| 231 |
0 |
float width = 320f; |
| 232 |
0 |
Integer intValue = (Integer)style.get( SelectItemButton.STYLE_WIDTH ); |
| 233 |
0 |
if( intValue != null ) { |
| 234 |
0 |
width = intValue.floatValue(); |
| 235 |
|
} |
| 236 |
|
|
| 237 |
0 |
Font fontValue = (Font)style.get( SelectItemButton.STYLE_FONT ); |
| 238 |
0 |
if( fontValue != null ) { |
| 239 |
0 |
setFont( fontValue ); |
| 240 |
|
} |
| 241 |
|
|
| 242 |
0 |
prepareLabel( width, 2 ); |
| 243 |
0 |
} |
| 244 |
|
|
| 245 |
|
|
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
public void prepareLabel( float width, int maxRows ) |
| 251 |
|
{ |
| 252 |
0 |
if( _item == null ) { |
| 253 |
0 |
Logger.error( "[canvas.text] not specified corresponding SelectItem." ); |
| 254 |
0 |
return; |
| 255 |
|
} |
| 256 |
|
|
| 257 |
0 |
Graphics2D g2 = (Graphics2D)PekoSystem.getInstance().getMainWindow().getGraphics(); |
| 258 |
0 |
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); |
| 259 |
|
|
| 260 |
0 |
List lines = new java.util.ArrayList(); |
| 261 |
0 |
float lineHeight = 0f; |
| 262 |
|
|
| 263 |
0 |
String text = _item.getText(); |
| 264 |
0 |
AttributedString astring = new AttributedString( text ); |
| 265 |
0 |
astring.addAttribute( TextAttribute.FONT, getFont() ); |
| 266 |
0 |
LineBreakMeasurer lbm = new LineBreakMeasurer( astring.getIterator(), g2.getFontRenderContext() ); |
| 267 |
0 |
int length = text.length(); |
| 268 |
0 |
Line line = null; |
| 269 |
0 |
for( int i = 0; (i < maxRows) && (lbm.getPosition() < length); ++i ) { |
| 270 |
0 |
line = new Line(); |
| 271 |
0 |
line.setTextLayout( lbm.nextLayout(width) ); |
| 272 |
0 |
line.setShadowColor( _shadow ); |
| 273 |
0 |
lines.add( line ); |
| 274 |
0 |
lineHeight += line.getAscent(); |
| 275 |
0 |
lineHeight += line.getDescent(); |
| 276 |
|
} |
| 277 |
0 |
_lines = lines; |
| 278 |
|
|
| 279 |
0 |
Dimension componentSize = new Dimension( (int)width, (class="keyword">int)lineHeight + 10 ); |
| 280 |
0 |
setPreferredSize( componentSize ); |
| 281 |
0 |
setSize( componentSize ); |
| 282 |
0 |
} |
| 283 |
|
|
| 284 |
|
|
| 285 |
|
|
| 286 |
|
|
| 287 |
|
|
| 288 |
|
public void setSelected( boolean selected ) |
| 289 |
|
{ |
| 290 |
0 |
if( _selected == selected ) { |
| 291 |
0 |
return; |
| 292 |
|
} |
| 293 |
|
|
| 294 |
0 |
if( selected ) { |
| 295 |
0 |
setForeground( _foregroundActive ); |
| 296 |
0 |
_owner.itemSelecting( this ); |
| 297 |
0 |
} |
| 298 |
|
else { |
| 299 |
0 |
setForeground( _foregroundNonActive ); |
| 300 |
0 |
_owner.itemDeselected( this ); |
| 301 |
|
} |
| 302 |
0 |
_selected = selected; |
| 303 |
0 |
repaint(); |
| 304 |
0 |
} |
| 305 |
|
|
| 306 |
|
|
| 307 |
|
|
| 308 |
|
|
| 309 |
|
|
| 310 |
|
public void paintComponent( Graphics g ) |
| 311 |
|
{ |
| 312 |
0 |
Graphics2D g2 = (Graphics2D)g.create(); |
| 313 |
0 |
g2.setComposite( _alphaComposite ); |
| 314 |
0 |
g2.setColor( getBackground() ); |
| 315 |
0 |
Dimension size = getSize( _size ); |
| 316 |
0 |
g2.fillRect( 0, 0, size.width, size.height ); |
| 317 |
|
|
| 318 |
0 |
List lines = _lines; |
| 319 |
0 |
if( lines != null ) { |
| 320 |
0 |
g2 = (Graphics2D)g; |
| 321 |
0 |
int length = lines.size(); |
| 322 |
0 |
float x = 5f; |
| 323 |
0 |
float y = 5f; |
| 324 |
0 |
Line line = null; |
| 325 |
0 |
for( int i = 0; i < length; ++i ) { |
| 326 |
0 |
line = (Line)lines.get( i ); |
| 327 |
0 |
line.setForeground( getForeground() ); |
| 328 |
0 |
y += line.getAscent(); |
| 329 |
0 |
line.draw( g2, x, y ); |
| 330 |
0 |
y += line.getDescent(); |
| 331 |
|
} |
| 332 |
|
} |
| 333 |
0 |
} |
| 334 |
|
|
| 335 |
|
|
| 336 |
|
|
| 337 |
|
|
| 338 |
|
public void mousePressed( MouseEvent ev ) |
| 339 |
|
{ |
| 340 |
0 |
} |
| 341 |
|
|
| 342 |
|
public void mouseReleased( MouseEvent ev ) |
| 343 |
|
{ |
| 344 |
0 |
} |
| 345 |
|
|
| 346 |
|
public void mouseEntered( MouseEvent ev ) |
| 347 |
|
{ |
| 348 |
0 |
setSelected( true ); |
| 349 |
0 |
} |
| 350 |
|
|
| 351 |
|
public void mouseExited( MouseEvent ev ) |
| 352 |
|
{ |
| 353 |
0 |
setSelected( false ); |
| 354 |
0 |
} |
| 355 |
|
|
| 356 |
|
public void mouseClicked( MouseEvent ev ) |
| 357 |
|
{ |
| 358 |
0 |
if( _selected && (ev.getModclass="keyword">ifiers() == MouseEvent.BUTTON1_MASK) ) { |
| 359 |
0 |
_owner.itemSelected( this ); |
| 360 |
|
} |
| 361 |
0 |
} |
| 362 |
|
|
| 363 |
|
} |