Coverage Report - org.argouml.ui.StylePanelFigText
 
Classes in this File Line Coverage Branch Coverage Complexity
StylePanelFigText
0%
0/145
0%
0/64
5.444
 
 1  
 /* $Id: StylePanelFigText.java 18555 2010-07-24 10:01:49Z bobtarling $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    mvw
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2008 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies. This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.ui;
 40  
 
 41  
 import java.awt.Color;
 42  
 import java.awt.event.ItemEvent;
 43  
 
 44  
 import javax.swing.JComboBox;
 45  
 import javax.swing.JLabel;
 46  
 
 47  
 import org.argouml.i18n.Translator;
 48  
 import org.argouml.ui.targetmanager.TargetManager;
 49  
 import org.tigris.gef.presentation.Fig;
 50  
 import org.tigris.gef.presentation.FigText;
 51  
 import org.tigris.gef.ui.ColorRenderer;
 52  
 
 53  
 /**
 54  
  * StylePanel class which provides additional parameters for changing Text
 55  
  * elements provided by GEF.
 56  
  *  TODO: i18n
 57  
  */
 58  
 public class StylePanelFigText extends StylePanelFig {
 59  
 
 60  
     /**
 61  
      * TODO: These can not simply be translated...
 62  
      */
 63  0
     private static final String[] FONT_NAMES = {
 64  
         "dialog", "serif", "sanserif",
 65  
         "monospaced",
 66  
     };
 67  
 
 68  0
     private static final Integer[] COMMON_SIZES = {
 69  
         Integer.valueOf(8), Integer.valueOf(9),
 70  
         Integer.valueOf(10), Integer.valueOf(12), 
 71  
         Integer.valueOf(16), Integer.valueOf(18),
 72  
         Integer.valueOf(24), Integer.valueOf(36), 
 73  
         Integer.valueOf(48), Integer.valueOf(72),
 74  
         Integer.valueOf(96),
 75  
     };
 76  
 
 77  0
     private static final String[] STYLES = {
 78  
         "Plain", "Bold", "Italic",
 79  
         "Bold-Italic",
 80  
     };
 81  
 
 82  0
     private static final String[] JUSTIFIES = {
 83  
         "Left", "Right", "Center",
 84  
     };
 85  
 
 86  0
     private JLabel fontLabel = new JLabel(
 87  
             Translator.localize("label.stylepane.font") + ": ");
 88  
 
 89  0
     private JComboBox fontField = new JComboBox(FONT_NAMES);
 90  
 
 91  0
     private JLabel sizeLabel = new JLabel(
 92  
             Translator.localize("label.stylepane.size") + ": ");
 93  
 
 94  0
     private JComboBox sizeField = new JComboBox(COMMON_SIZES);
 95  
 
 96  0
     private JLabel styleLabel = new JLabel(
 97  
             Translator.localize("label.stylepane.style") + ": ");
 98  
 
 99  0
     private JComboBox styleField = new JComboBox(STYLES);
 100  
 
 101  0
     private JLabel justLabel = new JLabel(
 102  
             Translator.localize("label.stylepane.justify") + ": ");
 103  
 
 104  0
     private JComboBox justField = new JComboBox(JUSTIFIES);
 105  
 
 106  0
     private JLabel textColorLabel = new JLabel(
 107  
             Translator.localize("label.stylepane.text-color") + ": ");
 108  
 
 109  0
     private JComboBox textColorField = new JComboBox();
 110  
 
 111  
     /**
 112  
      * Construct default style panel for text elements.
 113  
      */
 114  
     public StylePanelFigText() {
 115  0
         super();
 116  
 
 117  0
         fontField.addItemListener(this);
 118  0
         sizeField.addItemListener(this);
 119  0
         styleField.addItemListener(this);
 120  0
         justField.addItemListener(this);
 121  0
         textColorField.addItemListener(this);
 122  
 
 123  0
         textColorField.setRenderer(new ColorRenderer());
 124  
 
 125  0
         textColorLabel.setLabelFor(textColorField);
 126  0
         add(textColorLabel);
 127  0
         add(textColorField);
 128  
 
 129  0
         addSeperator();
 130  
 
 131  0
         fontLabel.setLabelFor(fontField);
 132  0
         add(fontLabel);
 133  0
         add(fontField);
 134  
 
 135  0
         sizeLabel.setLabelFor(sizeField);
 136  0
         add(sizeLabel);
 137  0
         add(sizeField);
 138  
 
 139  0
         styleLabel.setLabelFor(styleField);
 140  0
         add(styleLabel);
 141  0
         add(styleField);
 142  
 
 143  0
         justLabel.setLabelFor(justField);
 144  0
         add(justLabel);
 145  0
         add(justField);
 146  
 
 147  0
         initChoices2();
 148  0
     }
 149  
 
 150  
     /**
 151  
      * Second part of the default style panel construction.
 152  
      */
 153  
     protected void initChoices2() {
 154  0
         textColorField.addItem(Color.black);
 155  0
         textColorField.addItem(Color.white);
 156  0
         textColorField.addItem(Color.gray);
 157  0
         textColorField.addItem(Color.lightGray);
 158  0
         textColorField.addItem(Color.darkGray);
 159  0
         textColorField.addItem(Color.red);
 160  0
         textColorField.addItem(Color.blue);
 161  0
         textColorField.addItem(Color.green);
 162  0
         textColorField.addItem(Color.orange);
 163  0
         textColorField.addItem(Color.pink);
 164  0
         textColorField.addItem(getCustomItemName());
 165  
 
 166  0
     }
 167  
 
 168  
     /**
 169  
      * Refresh the style panel when the Fig has been altered.
 170  
      *
 171  
      * @see org.argouml.ui.TabTarget#refresh()
 172  
      */
 173  
     public void refresh() {
 174  0
         if (TargetManager.getInstance().getTargets().size() > 1) {
 175  
             // See issue 6109 - if we have multiple targets this method
 176  
             // can result in a feedback problem where selecting a target
 177  
             // changes the selection colour in the combo and as a result
 178  
             // that trigger a change of colour of all selected Figs
 179  0
             return;
 180  
         }
 181  0
         super.refresh();
 182  0
         FigText ft = (FigText) getPanelTarget();
 183  0
         if (ft == null) {
 184  0
             return;
 185  
         }
 186  0
         String fontName = ft.getFontFamily().toLowerCase();
 187  0
         int size = ft.getFontSize();
 188  0
         String styleName = STYLES[0];
 189  
 
 190  0
         fontField.setSelectedItem(fontName);
 191  0
         sizeField.setSelectedItem(Integer.valueOf(size));
 192  0
         if (ft.getBold()) {
 193  0
             styleName = STYLES[1];
 194  
         }
 195  0
         if (ft.getItalic()) {
 196  0
             styleName = STYLES[2];
 197  
         }
 198  0
         if (ft.getBold() && ft.getItalic()) {
 199  0
             styleName = STYLES[3];
 200  
         }
 201  0
         styleField.setSelectedItem(styleName);
 202  
 
 203  0
         String justName = JUSTIFIES[0];
 204  0
         int justCode = ft.getJustification();
 205  0
         if (justCode >= 0 && justCode <= JUSTIFIES.length) {
 206  0
             justName = JUSTIFIES[justCode];
 207  
         }
 208  0
         justField.setSelectedItem(justName);
 209  
 
 210  0
         Color c = ft.getTextColor();
 211  0
         textColorField.setSelectedItem(c);
 212  0
         if (c != null && !textColorField.getSelectedItem().equals(c)) {
 213  0
             textColorField.insertItemAt(c, textColorField.getItemCount() - 1);
 214  0
             textColorField.setSelectedItem(c);
 215  
         }
 216  
 
 217  0
         if (ft.isFilled()) {
 218  0
             Color fc = ft.getFillColor();
 219  0
             getFillField().setSelectedItem(fc);
 220  0
             if (fc != null && !getFillField().getSelectedItem().equals(fc)) {
 221  0
                 getFillField().insertItemAt(fc,
 222  
                                             getFillField().getItemCount() - 1);
 223  0
                 getFillField().setSelectedItem(fc);
 224  
             }
 225  0
         } else {
 226  0
             getFillField().setSelectedIndex(0);
 227  
         }
 228  0
     }
 229  
 
 230  
     /**
 231  
      * Set the font of the text element to the selected value.
 232  
      */
 233  
     protected void setTargetFont() {
 234  0
         if (getPanelTarget() == null) {
 235  0
             return;
 236  
         }
 237  0
         String fontStr = (String) fontField.getSelectedItem();
 238  0
         if (fontStr.length() == 0) {
 239  0
             return;
 240  
         }
 241  0
         ((FigText) getPanelTarget()).setFontFamily(fontStr);
 242  0
         getPanelTarget().endTrans();
 243  0
     }
 244  
 
 245  
     /**
 246  
      * Change font size of the text element according to the selected value.
 247  
      */
 248  
     protected void setTargetSize() {
 249  0
         if (getPanelTarget() == null) {
 250  0
             return;
 251  
         }
 252  0
         Integer size = (Integer) sizeField.getSelectedItem();
 253  0
         ((FigText) getPanelTarget()).setFontSize(size.intValue());
 254  0
         getPanelTarget().endTrans();
 255  0
     }
 256  
 
 257  
     /**
 258  
      * Change style of the text element (bold/italic) according to the selected
 259  
      * value.
 260  
      */
 261  
     protected void setTargetStyle() {
 262  0
         if (getPanelTarget() == null) {
 263  0
             return;
 264  
         }
 265  0
         String styleStr = (String) styleField.getSelectedItem();
 266  0
         if (styleStr == null) {
 267  0
             return;
 268  
         }
 269  0
         boolean bold = (styleStr.indexOf("Bold") != -1);
 270  0
         boolean italic = (styleStr.indexOf("Italic") != -1);
 271  0
         ((FigText) getPanelTarget()).setBold(bold);
 272  0
         ((FigText) getPanelTarget()).setItalic(italic);
 273  0
         getPanelTarget().endTrans();
 274  0
     }
 275  
 
 276  
     /**
 277  
      * Change the justification of the text according to the selected value.
 278  
      */
 279  
     protected void setTargetJustification() {
 280  0
         if (getPanelTarget() == null) {
 281  0
             return;
 282  
         }
 283  0
         String justStr = (String) justField.getSelectedItem();
 284  0
         if (justStr == null) {
 285  0
             return;
 286  
         }
 287  0
         ((FigText) getPanelTarget()).setJustificationByName(justStr);
 288  0
         getPanelTarget().endTrans();
 289  0
     }
 290  
 
 291  
 
 292  
 
 293  
     /**
 294  
      * Change the color of the text element according to the selected value.
 295  
      */
 296  
     protected void setTargetTextColor() {
 297  0
         if (getPanelTarget() == null) {
 298  0
             return;
 299  
         }
 300  0
         Object c = textColorField.getSelectedItem();
 301  0
         if (c instanceof Color) {
 302  0
             ((FigText) getPanelTarget()).setTextColor((Color) c);
 303  
         }
 304  0
         getPanelTarget().endTrans();
 305  0
     }
 306  
 
 307  
     /*
 308  
      * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
 309  
      */
 310  
     public void itemStateChanged(ItemEvent e) {
 311  0
         Object src = e.getSource();
 312  0
         Fig target = getPanelTarget();
 313  0
         if (e.getStateChange() == ItemEvent.SELECTED
 314  
                 && target instanceof FigText) {
 315  0
             if (src == fontField) {
 316  0
                 setTargetFont();
 317  0
             } else if (src == sizeField) {
 318  0
                 setTargetSize();
 319  0
             } else if (src == styleField) {
 320  0
                 setTargetStyle();
 321  0
             } else if (src == justField) {
 322  0
                 setTargetJustification();
 323  0
             } else if (src == textColorField) {
 324  0
                 if (e.getItem() == getCustomItemName()) {
 325  0
                     handleCustomColor(textColorField, 
 326  
                             "label.stylepane.custom-text-color",
 327  
                             ((FigText) target).getTextColor());
 328  
                 }
 329  0
                 setTargetTextColor();
 330  
             } else {
 331  0
                 super.itemStateChanged(e);
 332  
             }
 333  
         }
 334  0
     }
 335  
 
 336  
 
 337  
     /**
 338  
      * The UID.
 339  
      */
 340  
     private static final long serialVersionUID = 2019248527481196634L;
 341  
 } /* end class StylePanelFigText */