Java 6 SE Platform
6.0
Home

com.woven_media.colorsafe
Class Controller

java.lang.Object
  extended by com.woven_media.colorsafe.Controller
All Implemented Interfaces:
IPalette, java.util.EventListener, javax.swing.event.ChangeListener

public class Controller
extends java.lang.Object
implements javax.swing.event.ChangeListener, IPalette

Controller class handles UI and event-handling.

Since:
1.0

Field Summary
private  javax.swing.JButton applyBtn
           
private  java.awt.Font bf
           
private static java.awt.Color bgColor
           
private  javax.swing.JPanel bigUpperPanel
           
private  javax.swing.JLabel bLabel
           
private static java.awt.Color c
           
private  javax.swing.JButton cancelBtn
           
private  javax.swing.JCheckBox chkBox
           
private  javax.swing.JPanel colorInfoPanel
           
private  javax.swing.JPanel colorSafeSwatch
           
private  javax.swing.JPanel colorSwatch
           
private  int controlsLength
           
private  java.awt.Container cp
           
private static java.awt.Color currSel
           
private static java.awt.Color curSafe
           
static java.lang.String DEF_RES_PATH
          Constants
static java.awt.Color DEFAULT_SWATCH
           
private  javax.swing.border.Border etched
           
private  java.lang.String fieldValue
           
private static javax.swing.ImageIcon flagDE
           
private static javax.swing.ImageIcon flagUS
           
private  javax.swing.JTextField fldBlue
           
private  javax.swing.JTextField fldGreen
           
private  javax.swing.JTextField fldHex
           
private  javax.swing.JTextField fldRed
           
private  javax.swing.JLabel gLabel
           
private  ColorUtil hex
           
private  boolean hexCalc
           
private  javax.swing.JLabel hLabel
           
private static javax.swing.JFrame host
          Private Members
private static javax.swing.ImageIcon iconImg
           
private  ColorInfoPanel info
           
private  javax.swing.border.TitledBorder infoBorder
           
private static java.awt.Color infoColor
           
private  boolean init
           
private  javax.swing.JTextField[] jFields
           
private  javax.swing.JLabel[] jLabels
           
private  javax.swing.JSlider[] jSliders
           
private  javax.swing.JPanel levelPanel
           
private static java.util.Locale locale
           
private  javax.swing.JPanel lPanel
           
private  javax.swing.JMenuItem[] mi
           
private static java.awt.Color newColor
           
private  int num
           
private  double num2
           
private  java.util.Vector<java.awt.Component> order
           
private  javax.swing.border.TitledBorder panelBorder
           
private  javax.swing.JPopupMenu popup
           
private static java.util.ResourceBundle rb
           
private  javax.swing.JButton resetBtn
           
private  javax.swing.JLabel rLabel
           
private  javax.swing.JLabel safeSwatchLabel
           
private  javax.swing.JSlider sliderBlu
           
private  javax.swing.JSlider sliderEmp
           
private  javax.swing.JSlider sliderGrn
           
private  javax.swing.JSlider sliderRed
           
private static java.awt.Color swatchDefault
           
private  javax.swing.JLabel swatchLabel
           
private  javax.swing.JPanel swatchPanel
           
static int TXT_MAXLENGTH
           
private static java.net.URL url
           
 
Fields inherited from interface com.woven_media.colorsafe.IPalette
BG_VAL, GRAYSCALE_NUM_COLORS, GRAYSCALE_PALETTE, GRAYSCALE_TITLE, HEX_VAL, NAMED_COLORS_PALETTE, NAMED_COLORS_TITLE, NAMED_NUM_COLORS, NETSCAPE_NUM_COLORS, NETSCAPE_PALETTE, NETSCAPE_TITLE, RGB_VAL, WIN_GRAYS_NUM_COLORS, WIN_NUM_COLORS, WINDOWS_GRAYS_PALETTE, WINDOWS_GRAYS_TITLE, WINDOWS_PALETTE, WINDOWS_TITLE, X11_NUM_COLORS, X11_PALETTE, X11_TITLE
 
Constructor Summary
Controller(javax.swing.JFrame f)
          Class constructor sets up the javax.swing.JFrame and calls Controller.start.
 
Method Summary
 void applyAction()
          Applies all changes made within the RGB and Hex text fields.
 void colorPicker(java.lang.String title, int selection)
          Opens a javax.swing.JColorChooser dialog.
private  void colorSelection(int selection)
           
private  void createComponents()
          Instantiates components for the content pane; includes buttons, sliders, panels, labels, text fields, popup and borders.
private  void createMenu(javax.swing.JFrame host)
          Creates the top-level menubar and specifies the host window.
private  void createUI()
          Sets and lays out the instantiated components for the GUI.
protected  void finalValidation(java.awt.event.FocusEvent fe, javax.swing.JTextField txt)
          Completes validation for the RGB text fields.
static java.awt.Color getBgColor()
          Returns this app's background color.
 javax.swing.JTextField getBlueField()
           
 java.awt.Color getChooserColor(int selection)
          Returns the current color selection from the javax.swing.JColorChooser.
 java.awt.Color getCurrentSafeColor()
          Returns the swatch panel's current selected safe color.
 java.awt.Color getCurrentSelectedColor()
          Returns the swatch panel's current selected color.
 javax.swing.JTextField getGreenField()
           
static javax.swing.ImageIcon getImageIcon()
          Returns this instance's icon image.
 java.awt.Insets getInsets()
          Returns the insets for GridBag padding of components in this window.
static java.util.Locale getLocale()
          Returns the Controller.locale member.
 javax.swing.JTextField getRedField()
           
static java.util.ResourceBundle getResources()
          Reads in the java.util.ResourceBundle strings_xx.properties file used by this GUI instance.
 java.awt.Color getRGBColor()
          Reads the values from the RGB text fields and converts and returns java.awt.Color object via the ColorUtil class.
 java.awt.Color getSafeColor(int[] rgbArray)
          Returns the nearest safe color based on the selected color.
 java.net.URL getURL()
          Returns the Controller.url member.
static void getURLResource(java.lang.String res)
          Sets the Controller.url member.
 boolean isArithmeticConversion()
          Determines selection of the Arithmetic conversion checkbox.
 boolean isSafeColor()
           
static void loadIcon(javax.swing.JFrame f)
          Loads image icon from default resources.
 void msgDialog(java.awt.Component parent, java.lang.Object msg, java.lang.String title, int msgType)
          Handles error, warning and information messages by opening a dialog.
 void openColorPalette(java.lang.String title, int palette)
          Opens the palette windows.
 int[] parseRGBfields()
          Parses the RGB string textfield values for floating point or integer values.
private  void removeComponents()
          Removes javax.swing.JPanels from the content pane.
 void resetAction()
          Resets this application back to its default values.
 void restart()
          Calls Controller.start.
static void setBgColor(java.awt.Color c)
          Sets this app's background color.
 void setCurrentColor(java.awt.Color c)
          Sets the current color selection.
 void setHexText(java.lang.String str)
          Sets the text value of the Hex text field.
static void setLocale(java.util.Locale loc)
          Sets Controller.locale.
 void setRGBcontrols()
          Sets RGB textfields and sliders to the correct values based on the value of the Hex textfield.
 void setSelection(java.awt.event.FocusEvent fe)
          Sets selection for text entered into javax.swing.JTextFields.
private  java.awt.FocusTraversalPolicy setTabOrder()
          Handles the focus cycle and tab order for this instance.
 void showAboutBox()
          Displays the About object in a bordless window - called from Help menu.
 void showColor(java.awt.Color c)
          Displays selected color for this instance.
 void showHexString()
          Sets the hexadecimal value in the javax.swing.JTextField.
 void showSafeColor()
          Displays the current color information.
private  void start()
          Starts the application.
 void stateChanged(javax.swing.event.ChangeEvent e)
          Receives stateChanged events from this instance's javax.swing.JSlider components.
 void textFieldGainedFocus(java.awt.event.FocusEvent fe, javax.swing.JTextField txt)
          Handle actions when a javax.swing.JtextField gains focus.
 void textFieldLostFocus(java.awt.event.FocusEvent fe, javax.swing.JTextField txt)
          Handles actions when a javax.swing.JtextField loses focus.
protected  void validateTextField(java.awt.event.KeyEvent e)
          Sets the maximum number of characters that are allowed to be entered into the textfields.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEF_RES_PATH

public static final java.lang.String DEF_RES_PATH
Constants

See Also:
Constant Field Values

DEFAULT_SWATCH

public static final java.awt.Color DEFAULT_SWATCH

TXT_MAXLENGTH

public static final int TXT_MAXLENGTH
See Also:
Constant Field Values

host

private static javax.swing.JFrame host
Private Members


colorSwatch

private javax.swing.JPanel colorSwatch

colorSafeSwatch

private javax.swing.JPanel colorSafeSwatch

lPanel

private javax.swing.JPanel lPanel

levelPanel

private javax.swing.JPanel levelPanel

bigUpperPanel

private javax.swing.JPanel bigUpperPanel

swatchPanel

private javax.swing.JPanel swatchPanel

colorInfoPanel

private javax.swing.JPanel colorInfoPanel

cancelBtn

private javax.swing.JButton cancelBtn

resetBtn

private javax.swing.JButton resetBtn

applyBtn

private javax.swing.JButton applyBtn

fldRed

private javax.swing.JTextField fldRed

fldGreen

private javax.swing.JTextField fldGreen

fldBlue

private javax.swing.JTextField fldBlue

fldHex

private javax.swing.JTextField fldHex

sliderRed

private javax.swing.JSlider sliderRed

sliderGrn

private javax.swing.JSlider sliderGrn

sliderBlu

private javax.swing.JSlider sliderBlu

sliderEmp

private javax.swing.JSlider sliderEmp

rLabel

private javax.swing.JLabel rLabel

gLabel

private javax.swing.JLabel gLabel

bLabel

private javax.swing.JLabel bLabel

hLabel

private javax.swing.JLabel hLabel

swatchLabel

private javax.swing.JLabel swatchLabel

safeSwatchLabel

private javax.swing.JLabel safeSwatchLabel

panelBorder

private javax.swing.border.TitledBorder panelBorder

infoBorder

private javax.swing.border.TitledBorder infoBorder

popup

private javax.swing.JPopupMenu popup

chkBox

private javax.swing.JCheckBox chkBox

jFields

private javax.swing.JTextField[] jFields

jSliders

private javax.swing.JSlider[] jSliders

jLabels

private javax.swing.JLabel[] jLabels

mi

private javax.swing.JMenuItem[] mi

num2

private double num2

controlsLength

private int controlsLength

num

private int num

hexCalc

private boolean hexCalc

init

private boolean init

fieldValue

private java.lang.String fieldValue

hex

private ColorUtil hex

info

private ColorInfoPanel info

bf

private java.awt.Font bf

etched

private javax.swing.border.Border etched

cp

private java.awt.Container cp

order

private java.util.Vector<java.awt.Component> order

rb

private static java.util.ResourceBundle rb

locale

private static java.util.Locale locale

url

private static java.net.URL url

c

private static java.awt.Color c

bgColor

private static java.awt.Color bgColor

infoColor

private static java.awt.Color infoColor

newColor

private static java.awt.Color newColor

currSel

private static java.awt.Color currSel

curSafe

private static java.awt.Color curSafe

swatchDefault

private static java.awt.Color swatchDefault

iconImg

private static javax.swing.ImageIcon iconImg

flagUS

private static javax.swing.ImageIcon flagUS

flagDE

private static javax.swing.ImageIcon flagDE
Constructor Detail

Controller

public Controller(javax.swing.JFrame f)
Class constructor sets up the javax.swing.JFrame and calls Controller.start. The constructor sets an init flag to true indicating that this class is being initialized, then sets background color and loads the image icon.

Parameters:
f - javax.swing.JFrame reference to the parent window.
Since:
1.0
Method Detail

start

private void start()
Starts the application. The start method grabs the resource bundle and sets up the UI, if the init flag is false, the start method is not being called by the constructor and the this.removeComponents method is called to reload the components in the content pane. This behavior is required for toggling language options at runtime, since the components need to be reloaded with new string values from the resource bundle's strings_xx.properties file. The xx suffix specifies the standardized two-letter lowercase language code.

e.g.

English
strings_en.properties
German
strings_de.properties
French
strings_fr.properties
...etc.

Adds the menubar and all the UI components. Calls resetAction to setup default color settings.

NOTE: The host reference needs to call the javax.swing.JFrame#pack method to set the preferred size and the layouts of its subcomponents.

Since:
1.0

restart

public void restart()
Calls Controller.start. The restart method reloads this app instance without destroying the enclosing parent frame. Convenience method only. Used by the ColorSafeMenubar class to handle the language toggle event at runtime.

Since:
1.0

removeComponents

private void removeComponents()
Removes javax.swing.JPanels from the content pane.

Since:
1.0
See Also:
Container.remove(int)

setBgColor

public static void setBgColor(java.awt.Color c)
Sets this app's background color.

Parameters:
c - java.awt.Color object
Since:
1.0

getBgColor

public static java.awt.Color getBgColor()
Returns this app's background color.

Returns:
java.awt.Color object of the background color for this instance.
Since:
1.0

getImageIcon

public static javax.swing.ImageIcon getImageIcon()
Returns this instance's icon image.

Returns:
iconImg javax.swing.IconImage

getCurrentSelectedColor

public java.awt.Color getCurrentSelectedColor()
Returns the swatch panel's current selected color.

Returns:
java.awt.Color object of this swatch panel's background color if colorSwatch panel is not null, otherwise returns null.
Since:
1.0

getCurrentSafeColor

public java.awt.Color getCurrentSafeColor()
Returns the swatch panel's current selected safe color.

Returns:
java.awt.Color object of this safe swatch panel's background color if colorSafeSwatch panel is not null, otherwise returns null.
Since:
1.0

setLocale

public static void setLocale(java.util.Locale loc)
Sets Controller.locale. The locale setting is primarily used for multiple language support.

Parameters:
loc - a java.util.Locale object primarily used for setting language options.
Since:
1.0
See Also:
Locale

getLocale

public static java.util.Locale getLocale()
Returns the Controller.locale member.

Returns:
java.util.Locale object
Since:
1.0
See Also:
Locale

getResources

public static java.util.ResourceBundle getResources()
Reads in the java.util.ResourceBundle strings_xx.properties file used by this GUI instance.

Returns:
java.util.ResourceBundle used for localization support.
Since:
1.0
See Also:
ResourceBundle

getURLResource

public static void getURLResource(java.lang.String res)
Sets the Controller.url member. Locates relative path resources.

Parameters:
res - java.lang.String resource.
Since:
1.0
See Also:
URLClassLoader

getURL

public java.net.URL getURL()
Returns the Controller.url member.

Returns:
java.net.URL object.
Since:
1.0
See Also:
URL

loadIcon

public static void loadIcon(javax.swing.JFrame f)
Loads image icon from default resources.

Parameters:
f - parent javax.swing.JFrame reference.
Since:
1.0
See Also:
JFrame

createMenu

private void createMenu(javax.swing.JFrame host)
Creates the top-level menubar and specifies the host window.

Parameters:
host - parent javax.swing.JFrame reference.
Since:
1.0
See Also:
JMenuBar, ColorSafeMenuBar

createComponents

private void createComponents()
Instantiates components for the content pane; includes buttons, sliders, panels, labels, text fields, popup and borders.

Since:
1.0

createUI

private void createUI()
Sets and lays out the instantiated components for the GUI.

Since:
1.0

setTabOrder

private java.awt.FocusTraversalPolicy setTabOrder()
Handles the focus cycle and tab order for this instance.

Since:
1.0
See Also:
FocusTraversalPolicy, Vector, NewFocusTraversalPolicy

isSafeColor

public boolean isSafeColor()

setSelection

public void setSelection(java.awt.event.FocusEvent fe)
Sets selection for text entered into javax.swing.JTextFields.

Parameters:
fe - java.awt.event.FocusEvent
Since:
1.0
See Also:
JTextField, FocusEvent

applyAction

public void applyAction()
Applies all changes made within the RGB and Hex text fields. Opens a warning message in a dialog box if invalid input is detected.

Since:
1.0

resetAction

public void resetAction()
Resets this application back to its default values.

Since:
1.0

textFieldGainedFocus

public void textFieldGainedFocus(java.awt.event.FocusEvent fe,
                                 javax.swing.JTextField txt)
Handle actions when a javax.swing.JtextField gains focus.

Parameters:
fe - java.awt.event.FocusEvent
txt - javax.swing.JtextField reference of the field requesting focus.
Since:
1.0

textFieldLostFocus

public void textFieldLostFocus(java.awt.event.FocusEvent fe,
                               javax.swing.JTextField txt)
Handles actions when a javax.swing.JtextField loses focus.

Parameters:
fe - java.awt.event.FocusEvent
txt - javax.swing.JTextField
Since:
1.0

getRedField

public javax.swing.JTextField getRedField()

getGreenField

public javax.swing.JTextField getGreenField()

getBlueField

public javax.swing.JTextField getBlueField()

isArithmeticConversion

public boolean isArithmeticConversion()
Determines selection of the Arithmetic conversion checkbox.

Returns:
boolean value.private JTextField fldRed, fldGreen, fldBlue, fldHex;
Since:
1.0

showColor

public void showColor(java.awt.Color c)
Displays selected color for this instance.

Parameters:
c - java.awt.Color object.
Since:
1.0
See Also:
Color

getRGBColor

public java.awt.Color getRGBColor()
Reads the values from the RGB text fields and converts and returns java.awt.Color object via the ColorUtil class.

Returns:
java.awt.Color object
Since:
1.0

showSafeColor

public void showSafeColor()
Displays the current color information.

Since:
1.0
See Also:
ColorUtil

showHexString

public void showHexString()
Sets the hexadecimal value in the javax.swing.JTextField.

Since:
1.0

parseRGBfields

public int[] parseRGBfields()
Parses the RGB string textfield values for floating point or integer values.

Since:
1.0

stateChanged

public void stateChanged(javax.swing.event.ChangeEvent e)
Receives stateChanged events from this instance's javax.swing.JSlider components. Values of the text fields are adjusted according to current color selection.

Specified by:
stateChanged in interface javax.swing.event.ChangeListener
Parameters:
e - java.awt.event.ChangeEvent
Since:
1.0
See Also:
ChangeListener, ChangeEvent

setRGBcontrols

public void setRGBcontrols()
Sets RGB textfields and sliders to the correct values based on the value of the Hex textfield.

Since:
1.0
See Also:
JSlider

setCurrentColor

public void setCurrentColor(java.awt.Color c)
Sets the current color selection.

Parameters:
c - java.awt.Color object of the current selected color.
Since:
1.0

getSafeColor

public java.awt.Color getSafeColor(int[] rgbArray)
Returns the nearest safe color based on the selected color.

Parameters:
rgbArray - int array of RGB values
Returns:
java.awt.Color object
Since:
1.0

getChooserColor

public java.awt.Color getChooserColor(int selection)
Returns the current color selection from the javax.swing.JColorChooser.

Parameters:
selection - int value representing the current chooser color value.
Since:
1.0
See Also:
JColorChooser

colorPicker

public void colorPicker(java.lang.String title,
                        int selection)
Opens a javax.swing.JColorChooser dialog.

Parameters:
title - string for the title bar.
selection - int value for selected color.
Since:
1.0
See Also:
JColorChooser, JDialog, JRootPane

colorSelection

private void colorSelection(int selection)

openColorPalette

public void openColorPalette(java.lang.String title,
                             int palette)
Opens the palette windows. The palette windows calls the dispose method of javax.swing.JFrame instead of System.exit during window close events so that the parent window is not destroyed.

Parameters:
title - string for the title bar.
palette - int value representing the palette type.
Since:
1.0
See Also:
WindowAdapter, WindowEvent, ColorModelView

setHexText

public void setHexText(java.lang.String str)
Sets the text value of the Hex text field.

Since:
1.0
See Also:
JTextField

getInsets

public java.awt.Insets getInsets()
Returns the insets for GridBag padding of components in this window.

Returns:
java.awt.Insets object
Since:
1.0
See Also:
Insets

msgDialog

public void msgDialog(java.awt.Component parent,
                      java.lang.Object msg,
                      java.lang.String title,
                      int msgType)
Handles error, warning and information messages by opening a dialog.

The msgType parameter of the msgDialog can accept the following parameters,

Parameters:
parent - parent component for the dialog.
msg - message to disply in the content pane.
title - title bar text.
msgType - defines the style of the message.
Since:
1.0

showAboutBox

public void showAboutBox()
                  throws java.io.IOException
Displays the About object in a bordless window - called from Help menu. The About class is called statically since it is a stand-alone application containing a main method.

Throws:
java.io.IOException
Since:
1.0

validateTextField

protected void validateTextField(java.awt.event.KeyEvent e)
Sets the maximum number of characters that are allowed to be entered into the textfields.

Parameters:
e - java.awt.event.KeyEvent for text field validation.
Since:
1.0
See Also:
KeyEvent, Character

finalValidation

protected void finalValidation(java.awt.event.FocusEvent fe,
                               javax.swing.JTextField txt)
Completes validation for the RGB text fields. Prevents illegal values form being entered.

Parameters:
fe - java.awt.event.FocusEvent for text field validation.
txt - javax.swing.JTextField reference for validation.
Since:
1.0
See Also:
FocusEvent

Java 6 SE Platform
6.0
Home

JavaDoc API documentation written, prepared and compiled by Brent Allen Parrish, woven-media.com, 20 SEPT 2009