001    package echopoint.jquery;
002    
003    
004    import echopoint.internal.AbstractContainer;
005    import nextapp.echo.app.ImageReference;
006    import nextapp.echo.app.event.ActionEvent;
007    import nextapp.echo.app.event.ActionListener;
008    
009    import java.util.ArrayList;
010    import java.util.List;
011    
012    /**
013     *  A component to display a Mac-style dock menu (fisheye menu).
014     *
015     * The menu consists of a set of images that expand on mouse rollover. Along with
016     * the image a label can be displayed when the image is expanded.
017     *
018     * This component is using the jqDock project: http://www.wizzud.com/jqDock
019     * 
020     * @author Mikael S\u00f6derman 2009-06-03
021     * @version $Id$
022     */
023    public class DockMenu extends AbstractContainer {
024    
025        private List<ImageReference> inactiveImages;
026        //private List<ImageReference> activeImages; //todo
027        private List<ImageReference> rolloverImages;
028        private List<String> texts;
029        private List<String> actionCommands;
030        private List model;
031    
032        public static final String MODEL_CHANGED_PROPERTY = "model";
033        public static final String BUTTON_PRESSED_PROPERTY = "itemid";
034        public static final String PROPERTY_DOCK_WIDTH = "dockWidth";
035        public static final String PROPERTY_MINOR_AXIS_SIZE = "minorSize";
036        public static final String PROPERTY_SHOW_LABELS = "labels";
037        public static final String PROPERTY_DURATION = "duration";
038        public static final String PROPERTY_ALIGN = "align";
039        public static final String PROPERTY_COEFFICIENT = "coefficient";
040        public static final String PROPERTY_DISTANCE = "distance";
041    
042    
043        public static final String LABELS_BOTTOM_CENTER = "bc";
044        public static final String LABELS_TOP_LEFT = "tl";
045        public static final String LABELS_TOP_CENTER = "tc";
046        public static final String LABELS_TOP_RIGHT = "tr";
047        public static final String LABELS_MIDDLE_LEFT = "ml";
048        public static final String LABELS_MIDDLE_CENTER = "mc";
049        public static final String LABELS_MIDDLE_RIGHT = "mr";
050        public static final String LABELS_BOTTOM_LEFT = "bl";
051        public static final String LABELS_BOTTOM_RIGHT = "br";
052    
053        public static final String ALIGN_TOP = "top";
054        public static final String ALIGN_MIDDLE = "middle";
055        public static final String ALIGN_BOTTOM = "bottom";
056        public static final String ALIGN_LEFT = "left";
057        public static final String ALIGN_CENTER = "center";
058        public static final String ALIGN_RIGHT = "right";
059    
060        private int dockWidth = 0;
061    
062        /**
063         * Creates a new DockMenu instance.
064         *
065         * @param minorAxisSize The size (height for horizontal menus and width for vertical menus) for the item at rest
066         * @param displayLabels If set to null no labels will be displayed.
067         * @param duration  The duration of the expansion and shrinkage.
068         */
069        public DockMenu(int minorAxisSize, String displayLabels, int duration) {
070            this.model = new ArrayList();
071            this.inactiveImages = new ArrayList<ImageReference>();
072            //this.activeImages = new ArrayList<ImageReference>();
073            this.rolloverImages = new ArrayList<ImageReference>();
074            this.texts = new ArrayList<String>();
075            this.actionCommands = new ArrayList<String>();
076            this.model.add(texts);
077            this.model.add(actionCommands);
078            this.model.add(inactiveImages);
079            this.model.add(rolloverImages);
080           // this.model.add(activeImages);
081            set(PROPERTY_MINOR_AXIS_SIZE, minorAxisSize);
082            set(PROPERTY_DURATION, duration);
083            set(PROPERTY_ALIGN, ALIGN_BOTTOM);
084            set(PROPERTY_DISTANCE, new Integer(140));
085            set(PROPERTY_COEFFICIENT, new Float(1.1));
086    
087            if (displayLabels != null)
088                set(PROPERTY_SHOW_LABELS, displayLabels);
089            else
090                set(PROPERTY_SHOW_LABELS, false);
091    
092        }
093    
094        /**
095         * Adding an item to the menu. The item consists of an icon and possibly a label. The label
096         * is displayed when the menu item is expanded (i.e. on mouse rollover) but only if the
097         * DockMenu has been configured to display labels.
098         *
099         * @param text The text of the label.
100         * @param actionCommand The command to send in the ActionEvent when the item has been clicked
101         * @param imageWidth The width of the image when the menu item is fully expanded
102         * @param imageHeight The height of the image when the menu item is fully expanded
103         * @param icon The image to be displayed
104         * @param expanded The image to show at expanded state
105         */
106        public void addMenuItem(String text, String actionCommand, int imageWidth, int imageHeight,
107                                ImageReference icon, ImageReference expanded)
108        {
109            double resizeCoefficient = (double) imageHeight / (Integer) get(PROPERTY_MINOR_AXIS_SIZE);
110            int width = (int) (imageWidth / resizeCoefficient);
111            dockWidth = dockWidth + width;
112            set(PROPERTY_DOCK_WIDTH, dockWidth);
113    
114            inactiveImages.add(icon);
115            rolloverImages.add(expanded);
116            texts.add(text);
117            actionCommands.add(actionCommand);
118        }
119    
120        /**
121         * Returns the model
122         *
123         * @return the model
124         */
125        public List getModel() {
126            return model;
127        }
128    
129        public int getDockWidth()
130        {
131            return (Integer) get(PROPERTY_DOCK_WIDTH);
132        }
133    
134        public String getDisplayLabels()
135        {
136            return (String) get(PROPERTY_SHOW_LABELS);
137        }
138    
139        public Integer getDuration()
140        {
141            return (Integer) get(PROPERTY_DURATION);
142        }
143    
144        public void setCoefficient(Float coefficient)
145        {
146            set(PROPERTY_COEFFICIENT, coefficient);
147        }
148    
149        public Float getCoefficient()
150        {
151            return (Float) get(PROPERTY_COEFFICIENT);
152        }
153    
154        public Integer getDistance()
155        {
156            return (Integer) get(PROPERTY_DISTANCE);
157        }
158    
159        public void setDistance(Integer distance)
160        {
161            set(PROPERTY_DISTANCE, distance);
162        }
163    
164        public void setAlign(String align)
165        {
166            set(PROPERTY_ALIGN, align);
167        }
168    
169        public String getAlign()
170        {
171            return (String) get(PROPERTY_ALIGN);
172        }
173    
174        public void processInput(String inputName, Object inputValue) {
175            super.processInput(inputName, inputValue);
176            if (BUTTON_PRESSED_PROPERTY.equals(inputName)) {
177                String itemid = (String) inputValue;
178                fireActionPerformed(new ActionEvent(this, itemid));
179            }
180        }
181    
182        /**
183         * {@inheritDoc}
184         */
185        @Override
186        public void addActionListener(final ActionListener listener) {
187            super.addActionListener(listener);
188        }
189    
190        /**
191         * {@inheritDoc}
192         */
193        public void removeActionListener(final ActionListener listener) {
194            super.removeActionListener(listener);
195        }
196    
197        /**
198         * {@inheritDoc}.
199         */
200        @Override
201        public boolean hasActionListeners() {
202            return super.hasActionListeners();
203        }
204    
205    
206    }