001 package net.sourceforge.mp3dings;
002
003 /*
004 * Mp3dings - manage mp3 meta-information
005 * Copyright (C) 2003 Moritz Ringler
006 * $Id: NewPatternBuilder.java 43 2007-10-20 17:39:59Z ringler $
007 *
008 * This program is free software; you can redistribute it and/or
009 * modify it under the terms of the GNU General Public License
010 * as published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021 */
022 import java.awt.GridBagConstraints;
023 import java.awt.GridBagLayout;
024 import java.awt.GridLayout;
025 import java.awt.Rectangle;
026 import java.awt.BorderLayout;
027 import java.awt.Color;
028 import java.awt.Point;
029 import java.awt.Dimension;
030 import java.awt.Font;
031 import java.awt.Insets;
032 import java.awt.GraphicsEnvironment;
033 import java.awt.Toolkit;
034 import java.awt.Component;
035 import java.awt.event.FocusAdapter;
036 import java.awt.event.MouseAdapter;
037 import java.awt.event.ActionListener;
038 import java.awt.event.FocusEvent;
039 import java.awt.event.MouseEvent;
040 import java.awt.event.MouseListener;
041 import java.awt.event.FocusListener;
042 import java.awt.event.ActionEvent;
043 import java.io.IOException;
044 import java.text.DecimalFormat;
045 import java.text.DecimalFormatSymbols;
046 import java.text.NumberFormat;
047 import java.util.Locale;
048 import java.util.Vector;//must use Vector
049 import javax.swing.JOptionPane;
050 import javax.swing.JRadioButtonMenuItem;
051 import javax.swing.JRadioButton;
052 import javax.swing.BorderFactory;
053 import javax.swing.JTextField;
054 import javax.swing.JScrollPane;
055 import javax.swing.DefaultListCellRenderer;
056 import javax.swing.AbstractAction;
057 import javax.swing.ListSelectionModel;
058 import javax.swing.JList;
059 import javax.swing.ListCellRenderer;
060 import javax.swing.JPopupMenu;
061 import javax.swing.ButtonGroup;
062 import javax.swing.Action;
063 import javax.swing.JDialog;
064 import javax.swing.JButton;
065 import javax.swing.JMenu;
066 import javax.swing.JMenuItem;
067 import javax.swing.JComboBox;
068 import javax.swing.ActionMap;
069 import javax.swing.JPanel;
070 import javax.swing.SwingUtilities;
071 import javax.swing.JLabel;
072 import javax.swing.border.Border;
073 import javax.swing.event.ListDataEvent;
074 import javax.swing.event.ListDataListener;
075 import javax.swing.plaf.basic.BasicComboBoxRenderer;
076 import de.mospace.lang.BrowserLauncher;
077 import net.sourceforge.mp3dings.preferences.FieldFilterEditor;
078 import net.sourceforge.mp3dings.preferences.FilteredField;
079 import net.sourceforge.mp3dings.utils.CaseChanger;
080 import net.sourceforge.mp3dings.utils.RenamingPattern;
081 import net.sourceforge.mp3dings.utils.RenamingPattern.*;
082
083 /*
084 * TODO:
085 * - changeCase for StringVarBlocks
086 * - saving and loading
087 */
088 /**
089 * A GUI component that allows the user to create {@link
090 * net.sourceforge.mp3dings.utils.RenamingPattern renaming patterns} for mp3
091 * files. *
092 *
093 * @author ringler
094 */
095 public class NewPatternBuilder extends JPanel {
096 private final DataTableModel masterTM;
097 private Component eventSource = null;
098 /** The JList that renders the pattern blocks. */
099 protected JList patternView;
100 /** The JLabel that displays an example String */
101 protected JLabel examplePreview = new JLabel();
102 /** The pattern that is currently edited. */
103 protected RenamingPattern pattern;
104 private Point mouseXY;
105 private int mouseIndex;
106 private Block mouseBlock;
107 private final static Border CELL_BORDER = BorderFactory.createCompoundBorder(
108 BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(5, 2, 5, 3));
109
110 /** Description of the Field */
111 public final static int MOVE_LEFT = 0;
112 /** Description of the Field */
113 public final static int MOVE_RIGHT = 1;
114 /** Description of the Field */
115 public final static int MOVE_START = 2;
116 /** Description of the Field */
117 public final static int MOVE_END = 3;
118
119 private final static ListCellRenderer FIELD_SELECTOR_RENDERER =
120 new BasicComboBoxRenderer(){
121 public Component getListCellRendererComponent(JList list,
122 Object value,
123 int index,
124 boolean isSelected,
125 boolean cellHasFocus){
126 Object val = value;
127 if(value instanceof Field){
128 val = ((Field) value).longname();
129 }
130 return super.getListCellRendererComponent(list, val, index,
131 isSelected, cellHasFocus);
132 }
133
134 };
135
136 MouseListener contextMenuTrigger =
137 new MouseAdapter() {
138 public void mousePressed(MouseEvent e) {
139 maybeShowPopup(e);
140 }
141
142
143 public void mouseReleased(MouseEvent e) {
144 maybeShowPopup(e);
145 }
146
147
148 private void maybeShowPopup(MouseEvent e) {
149 if ((e.getSource() == patternView) &&
150 (e.isPopupTrigger())) {
151 mouseXY = e.getPoint();
152 mouseIndex = indexAt(mouseXY);
153 mouseBlock = getBlockAt(mouseXY);
154 if (mouseBlock != null) {
155 patternView.setSelectedIndex(mouseIndex);
156 }
157 JPopupMenu menu = contextMenu();
158 showPopup(menu, mouseXY);
159 }
160 }
161
162
163 private void showPopup(JPopupMenu popup, Point p) {
164 Toolkit tk = Toolkit.getDefaultToolkit();
165 Dimension scr = tk.getScreenSize();
166 Insets ins = tk.getScreenInsets(
167 GraphicsEnvironment.getLocalGraphicsEnvironment()
168 .getDefaultScreenDevice().getDefaultConfiguration());
169 Point screen = new Point(scr.width - ins.right,
170 scr.height - ins.bottom);
171 SwingUtilities.convertPointFromScreen(screen, patternView);
172 Dimension popupSize = popup.getPreferredSize();
173 popup.show(patternView,
174 (int) Math.min(p.getX(), screen.getX() - popupSize.width),
175 (int) Math.min(p.getY(), screen.getY() - popupSize.height));
176 }
177
178 };
179
180 ListDataListener exampleUpdater =
181 new ListDataListener() {
182 public void contentsChanged(ListDataEvent e) {
183 updateExample();
184 }
185
186
187 public void intervalAdded(ListDataEvent e) {
188 updateExample();
189 }
190
191
192 public void intervalRemoved(ListDataEvent e) {
193 updateExample();
194 }
195 };
196
197
198 /**
199 * Constructs a new PatternBuilder.
200 *
201 * @param model Description of the Parameter
202 * @param data Description of the Parameter
203 */
204 public NewPatternBuilder(RenamingPattern model, DataTableModel data) {
205 masterTM = data;
206 init(model);
207 }
208
209
210 private void init(RenamingPattern model) {
211 /*
212 * check pattern, use default if necessary
213 */
214 pattern = (model == null) ? new RenamingPattern() : model;
215 if (pattern.getSize() == 0) {
216 pattern.insert(0, new ConstantBlock(".mp3"));
217 pattern.insert(0, VarBlock.getInstance(Field.TITLE));
218 pattern.insert(0, new ConstantBlock(" - "));
219 pattern.insert(0, VarBlock.getInstance(Field.ARTIST));
220 }
221
222 /*
223 * configure patternView
224 */
225 patternView = new JList(pattern.asListModel());
226 patternView.setLayoutOrientation(JList.HORIZONTAL_WRAP);
227 patternView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
228 patternView.setVisibleRowCount(1);
229 patternView.addMouseListener(contextMenuTrigger);
230 //patternView.setBackground(Color.white);
231 //patternView.setForeground(Color.black);
232 //patternView.setSelectionBackground(new Color(200, 200, 255));
233 //patternView.setSelectionForeground(Color.black);
234 patternView.setCellRenderer(new BlockRenderer());
235 pattern.asListModel().addListDataListener(exampleUpdater);
236 JScrollPane scrollPane = new JScrollPane(patternView,
237 JScrollPane.VERTICAL_SCROLLBAR_NEVER,
238 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
239 setLayout(new BorderLayout());
240 add(scrollPane, BorderLayout.CENTER);
241 add(examplePreview, BorderLayout.SOUTH);
242
243 /*
244 * initalize context menu actions
245 */
246 initActions();
247
248 /*
249 * initialize example preview
250 */
251 updateExample();
252 }
253
254
255 /**
256 * Sets the pattern attribute of the NewPatternBuilder object
257 *
258 * @param model The new pattern value
259 */
260 public void setPattern(RenamingPattern model) {
261 pattern.asListModel().removeListDataListener(exampleUpdater);
262 patternView.setModel(model.asListModel());
263 model.asListModel().addListDataListener(exampleUpdater);
264 this.pattern = model;
265 updateExample();
266 patternView.repaint();
267 }
268
269
270 /**
271 * Gets the patternViewBounds attribute of the NewPatternBuilder object
272 *
273 * @return The patternViewBounds value
274 */
275 public Rectangle getPatternViewBounds() {
276 Rectangle bounds = patternView.getBounds();
277 bounds.setLocation(patternView.getLocationOnScreen());
278 return bounds;
279 }
280
281
282 private void initActions() {
283 ActionMap amap = getActionMap();
284 Action action;
285
286 /*
287 * new block
288 */
289 action =
290 new AbstractAction(Globals.getString("PATTERN_NEW_BLOCK")) {
291 public void actionPerformed(ActionEvent e) {
292 boolean insertBefore = isInLeftHalfCell(mouseXY);
293 final int newIndex = (insertBefore)
294 ? mouseIndex
295 : mouseIndex + 1;
296 eventSource = patternView;
297 insertNewBlockAt(newIndex);
298 }
299 };
300 amap.put("new_block", action);
301
302 /*
303 * delete
304 */
305 action =
306 new AbstractAction(Globals.getString("PATTERN_DELETE")) {
307 public void actionPerformed(ActionEvent e) {
308 deleteBlock(mouseIndex);
309 }
310 };
311 amap.put("delete", action);
312
313 /*
314 * edit const block
315 */
316 action =
317 new AbstractAction(Globals.getString("PATTERN_EDIT_CONSTANT_BLOCK")) {
318 public void actionPerformed(ActionEvent e) {
319 eventSource = patternView;
320 editConstantBlock((ConstantBlock) mouseBlock);
321 }
322 };
323 amap.put("edit_constant_block", action);
324
325 /*
326 * edit filters
327 */
328 action =
329 new AbstractAction(Globals.getString("PATTERN_EDIT_FILTERS")) {
330 public void actionPerformed(ActionEvent e) {
331 eventSource = patternView;
332 editFilters((VarBlock) mouseBlock);
333 }
334 };
335 amap.put("edit_filters", action);
336
337 /*
338 * edit number format
339 */
340 action =
341 new AbstractAction(Globals.getString("PATTERN_EDIT_NUMBER_FORMAT")) {
342 public void actionPerformed(ActionEvent e) {
343 eventSource = patternView;
344 editNumberFormat((NumberVarBlock) mouseBlock);
345 }
346 };
347 amap.put("edit_number_format", action);
348
349 /*
350 * move
351 */
352 amap.put("move_left", new MoveAction(MOVE_LEFT));
353 amap.put("move_right", new MoveAction(MOVE_RIGHT));
354 amap.put("move_start", new MoveAction(MOVE_START));
355 amap.put("move_end", new MoveAction(MOVE_END));
356 }
357
358
359 /**
360 * Gets the pattern attribute of the NewPatternBuilder object
361 *
362 * @return The pattern value
363 */
364 public RenamingPattern getPattern() {
365 return pattern;
366 }
367
368
369 /**
370 * Description of the Method
371 *
372 * @param point Description of the Parameter
373 * @return Description of the Return Value
374 */
375 protected int indexAt(Point point) {
376 return patternView.locationToIndex(point);
377 }
378
379
380 /**
381 * Gets the blockAt attribute of the NewPatternBuilder object
382 *
383 * @param p Description of the Parameter
384 * @return The blockAt value
385 */
386 protected Block getBlockAt(Point p) {
387 int index = patternView.locationToIndex(p);
388 Rectangle cellRectangle = patternView.getCellBounds(index, index);
389 Block b = null;
390 if (cellRectangle != null && cellRectangle.contains(p)) {
391 b = (Block) patternView.getModel().getElementAt(index);
392 }
393 return b;
394 }
395
396
397 /**
398 * Gets the inLeftHalfCell attribute of the NewPatternBuilder object
399 *
400 * @param p Description of the Parameter
401 * @return The inLeftHalfCell value
402 */
403 protected boolean isInLeftHalfCell(Point p) {
404 int index = patternView.locationToIndex(p);
405 Rectangle cellRectangle = patternView.getCellBounds(index, index);
406 boolean result = false;
407 if (cellRectangle != null) {
408 cellRectangle.width = (int) (0.5 * cellRectangle.width);
409 result = cellRectangle.contains(p);
410 }
411 return result;
412 }
413
414
415 /**
416 * Description of the Method
417 *
418 * @return Description of the Return Value
419 */
420 protected JPopupMenu contextMenu() {
421 ActionMap amap = getActionMap();
422 JPopupMenu result = new JPopupMenu();
423
424 result.add(amap.get("new_block"));
425
426 /*
427 * Click on block
428 */
429 if (mouseBlock != null) {
430
431 /*
432 * delete block
433 */
434 result.add(amap.get("delete"));
435
436 result.addSeparator();
437
438 if (mouseBlock instanceof ConstantBlock) {
439
440 /*
441 * edit constant block
442 */
443 result.add(amap.get("edit_constant_block"));
444
445 } else if (mouseBlock instanceof VarBlock) {
446
447 /*
448 * edit filters
449 */
450 result.add(amap.get("edit_filters"));
451
452 /*
453 * edit number format
454 */
455 if (mouseBlock instanceof NumberVarBlock) {
456 result.add(amap.get("edit_number_format"));
457 } else if (mouseBlock instanceof StringVarBlock) {
458 result.add(changeCaseMenu((StringVarBlock) mouseBlock));
459 }
460 }
461
462 result.addSeparator();
463
464 if (mouseIndex != 0) {
465 result.add(amap.get("move_left"));
466 }
467
468 int end = pattern.getSize() - 1;
469 if (mouseIndex != end) {
470 result.add(amap.get("move_right"));
471 }
472
473 if (mouseIndex > 1) {
474 result.add(amap.get("move_start"));
475 }
476
477 if (mouseIndex < end - 1) {
478 result.add(amap.get("move_end"));
479 }
480 }
481 return result;
482 }
483
484
485 private void deleteBlock(int index) {
486 pattern.remove(index);
487 }
488
489
490 private void insertNewBlockAt(int index) {
491 Block b = getNewBlock();
492 if (b != null) {
493 pattern.insert(index, b);
494 patternView.setSelectedIndex(index);
495 patternView.ensureIndexIsVisible(index);
496 }
497 }
498
499
500 private void editConstantBlock(ConstantBlock b) {
501 String newValue = (String) JOptionPane.showInputDialog(
502 NewPatternBuilder.this,
503 Globals.getString("PATTERN_EDIT_CONSTANT_BLOCK_TITLE"),
504 b.toString());
505 if (newValue != null) {
506 b.setValue(newValue);
507 updateExample();
508 patternView.repaint();
509 }
510 }
511
512
513 private void editFilters(VarBlock b) {
514 Field field = b.getField();
515 FilteredField ff = new FilteredField(field);
516 ff.setFilters(b.getFilters());
517 FieldFilterEditor ffe = new FieldFilterEditor(eventSource,
518 masterTM.filters.getFilterSet());
519 Point mousePoint = new Point(mouseXY);
520 SwingUtilities.convertPointToScreen(mousePoint, patternView);
521 b.setFilters(ffe.showDialog(ff, mousePoint, SwingUtilities.LEFT));
522 updateExample();
523 }
524
525
526 private void editNumberFormat(NumberVarBlock b) {
527 NumberFormat f = b.getFormat();
528 String fpattern = "#0.#";
529 DecimalFormatSymbols symb = null;
530 if (f instanceof DecimalFormat) {
531 DecimalFormat df = (DecimalFormat) f;
532 fpattern = df.toPattern();
533 symb = df.getDecimalFormatSymbols();
534 } else {
535 symb = new DecimalFormatSymbols(Locale.US);
536 }
537
538 JTextField dsInput = new JTextField(String.valueOf(symb.getDecimalSeparator()), 1);
539 dsInput.setFont(dsInput.getFont().deriveFont(Font.BOLD));
540 JLabel dsLabel = new JLabel(Globals.getString(
541 "PATTERN_EDIT_NUMBER_FORMAT_DECIMAL_SEPARATOR"));
542 dsLabel.setLabelFor(dsInput);
543 JTextField gsInput = new JTextField(String.valueOf(symb.getGroupingSeparator()), 1);
544 gsInput.setFont(gsInput.getFont().deriveFont(Font.BOLD));
545 JLabel gsLabel = new JLabel(Globals.getString(
546 "PATTERN_EDIT_NUMBER_FORMAT_GROUPING_SEPARATOR"));
547 gsLabel.setLabelFor(gsInput);
548
549 JTextField input = new JTextField(fpattern);
550
551 JLabel desc = new JLabel(
552 Globals.getString("PATTERN_EDIT_NUMBER_FORMAT_DESCRIPTION"));
553 final String uri =
554 "http://java.sun.com/docs/books/tutorial/"+
555 "i18n/format/numberpattern.html";
556 JButton browse = new JButton(uri);
557 browse.addActionListener(
558 new ActionListener() {
559 public void actionPerformed(ActionEvent e) {
560 try {
561 BrowserLauncher.openURL(uri);
562 } catch (IOException ex) {
563 JOptionPane.showMessageDialog(NewPatternBuilder.this,
564 Globals.getMessage("BROWSER_LAUNCHER_ERROR",
565 uri));
566 }
567 }
568 });
569
570 JPanel message = new JPanel(new GridBagLayout());
571 int row = 0;
572
573 GridBagConstraints gbc = new GridBagConstraints();
574 gbc.gridx = 0;
575 gbc.gridy = row++;
576 gbc.gridwidth = 2;
577 gbc.gridheight = 1;
578 gbc.anchor = GridBagConstraints.WEST;
579 gbc.fill = GridBagConstraints.BOTH;
580 gbc.weightx = 1.;
581 gbc.weighty = 1.;
582
583 message.add(desc, gbc);
584
585 gbc.gridy = row++;
586 message.add(browse, gbc);
587
588 Insets ins = gbc.insets;
589 gbc.insets = new Insets(5, 0, 5, 0);
590 gbc.gridy = row++;
591 message.add(input, gbc);
592
593 gbc.gridwidth = 1;
594 gbc.weightx = 0.;
595 gbc.gridy = row++;
596 message.add(dsLabel, gbc);
597
598 gbc.gridy = row--;
599 message.add(gsLabel, gbc);
600
601 gbc.weightx = 1.;
602 gbc.gridy = row++;
603 gbc.gridx = 1;
604 gbc.fill = GridBagConstraints.VERTICAL;
605 message.add(dsInput, gbc);
606
607 gbc.gridy = row++;
608 message.add(gsInput, gbc);
609
610 int result = JOptionPane.showOptionDialog(NewPatternBuilder.this,
611 message,
612 Globals.getString("PATTERN_EDIT_NUMBER_FORMAT"),
613 JOptionPane.OK_CANCEL_OPTION,
614 JOptionPane.QUESTION_MESSAGE,
615 null, null, null);
616
617 if (result == JOptionPane.OK_OPTION) {
618 String txt = gsInput.getText();
619 if (txt.length() > 0) {
620 symb.setGroupingSeparator(txt.charAt(0));
621 }
622 txt = dsInput.getText();
623 if (txt.length() > 0) {
624 symb.setDecimalSeparator(txt.charAt(0));
625 }
626 txt = input.getText();
627 try {
628 f = new DecimalFormat(txt, symb);
629 } catch (IllegalArgumentException ex) {
630 f = new DecimalFormat(fpattern, symb);
631 }
632 b.setFormat(f);
633 }
634 updateExample();
635 }
636
637
638 private JMenuItem menuItem(int ccMode, ActionListener al,
639 ButtonGroup bg, int activeMode) {
640 JRadioButtonMenuItem jmi = new JRadioButtonMenuItem(
641 CaseChanger.getModeName(ccMode));
642 jmi.setActionCommand(String.valueOf(ccMode));
643 jmi.addActionListener(al);
644 bg.add(jmi);
645 jmi.setSelected(ccMode == activeMode);
646 return jmi;
647 }
648
649
650 private JMenu changeCaseMenu(final StringVarBlock b) {
651 JMenu jm = new JMenu(Globals.getString("CONTEXT_MENU_CHANGE_CASE"));
652 jm.setMnemonic(((Integer)
653 Globals.getObject("CONTEXT_MENU_CHANGE_CASE_MNEMONICS")).intValue());
654 int m = CaseChanger.getNumModes();
655 ActionListener al =
656 new ActionListener() {
657 public void actionPerformed(ActionEvent e) {
658 int ccMode = -100;
659 try {
660 ccMode = Integer.parseInt(e.getActionCommand());
661 } catch (NumberFormatException ex) {
662 }
663 if (ccMode != -100) {
664 b.setCaseChangeMode(ccMode);
665 updateExample();
666 patternView.repaint();
667 }
668 }
669 };
670 ButtonGroup bg = new ButtonGroup();
671 int mode = b.getCaseChangeMode();
672 jm.add(menuItem(CaseChanger.NO_CHANGE, al, bg, mode));
673 jm.add(menuItem(CaseChanger.LOWER, al, bg, mode));
674 jm.add(menuItem(CaseChanger.UPPER, al, bg, mode));
675 jm.add(menuItem(CaseChanger.UPPER_FIRST, al, bg, mode));
676 jm.add(menuItem(CaseChanger.UPPER_EACH_FIRST, al, bg, mode));
677
678 return jm;
679 }
680
681
682 /** Description of the Method */
683 public void updateExample() {
684 examplePreview.setText(Globals.getMessage("PATTERN_EXAMPLE" ,
685 pattern.getExample()));
686 }
687
688
689 private Block getNewBlock() {
690 final ButtonGroup typeGroup = new ButtonGroup();
691 final JRadioButton constBlock = new JRadioButton(
692 Globals.getString("PATTERN_NEW_BLOCK_CONSTANT"));
693 final JRadioButton varBlock = new JRadioButton(
694 Globals.getString("PATTERN_NEW_BLOCK_VARIABLE"));
695 typeGroup.add(constBlock);
696 typeGroup.add(varBlock);
697
698 final JTextField constInput = new JTextField();
699 FocusListener autoSelect =
700 new FocusAdapter() {
701 public void focusGained(FocusEvent e) {
702 constBlock.setSelected(true);
703 }
704 };
705 constBlock.setSelected(true);
706 constInput.addFocusListener(autoSelect);
707 Vector admissibleFields = new Vector();//must use Vector
708 Field[] fields = masterTM.getFields();
709 final int fn = fields.length;
710 for (int i = 0; i < fn; i++) {
711 int type = fields[i].type();
712 if (type == Field.TYPE_NUMERIC ||
713 type == Field.TYPE_STRING ||
714 type == Field.TYPE_LONGTEXT) {
715 admissibleFields.add(fields[i]);
716 }
717 }
718 final JComboBox varInput = new JComboBox(admissibleFields);
719 varInput.setRenderer(FIELD_SELECTOR_RENDERER);
720 autoSelect =
721 new FocusAdapter() {
722 public void focusGained(FocusEvent e) {
723 varBlock.setSelected(true);
724 }
725 };
726 varInput.addFocusListener(autoSelect);
727
728 JPanel panel = new JPanel(new GridLayout(2, 2));
729 panel.add(constBlock);
730 panel.add(constInput);
731 panel.add(varBlock);
732 panel.add(varInput);
733
734 JOptionPane optionPane = new JOptionPane(panel,
735 JOptionPane.QUESTION_MESSAGE,
736 JOptionPane.OK_CANCEL_OPTION);
737 JDialog dialog = optionPane.createDialog(
738 NewPatternBuilder.this,
739 Globals.getString("PATTERN_NEW_BLOCK"));
740 dialog.pack();
741 constInput.requestFocusInWindow();
742 dialog.setVisible(true);
743 dialog.dispose();
744 Block newBlock = null;
745 Integer result = (Integer) optionPane.getValue();
746 int resi = (result == null)
747 ? JOptionPane.CANCEL_OPTION
748 : result.intValue();
749 if (resi == JOptionPane.OK_OPTION) {
750 if (constBlock.isSelected()) {
751 newBlock = new ConstantBlock(constInput.getText());
752 } else {
753 newBlock = VarBlock.getInstance(
754 (Field) varInput.getSelectedItem());
755 }
756 }
757 return newBlock;
758 }
759
760
761 /**
762 * Description of the Class
763 *
764 * @author ringler
765 */
766 private static class BlockRenderer extends DefaultListCellRenderer {
767 private int listHeight = 0;
768 public static final int MAX_CELL_WIDTH = 75;
769 public static final int MIN_CELL_WIDTH = 60;
770 public static final int MIN_CELL_HEIGHT = 43;
771 private Color listSelectionForeground = null;
772 private Color listForeground = null;
773 private Color filteredForeground = null;
774 private Color unfilteredForeground = null;
775 private Color filteredSelectionForeground = null;
776 private Color unfilteredSelectionForeground = null;
777
778
779 /** Constructs a new Renderer. */
780 public BlockRenderer() { }
781
782
783 /**
784 * Returns the JLabel's width and the greater number out of the JLabel's
785 * height and the JList's height. Thus we avoid ugly empty space at the
786 * bottom of our single-row list.
787 *
788 * @return The preferredSize value
789 */
790 public Dimension getPreferredSize() {
791 Dimension d = super.getPreferredSize();
792 if (d.height < MIN_CELL_HEIGHT){
793 d.height = MIN_CELL_HEIGHT;
794 }
795 if (d.height < listHeight) {
796 d.height = listHeight;
797 }
798 if(d.width > MAX_CELL_WIDTH){
799 d.width = MAX_CELL_WIDTH;
800 } else if(d.width < MIN_CELL_WIDTH){
801 d.width = MIN_CELL_WIDTH;
802 }
803 return d;
804 }
805
806 // private static Color mixColors(Color bColor, Color hsColor){
807 // float[] hsbvals = new float[3];
808 // float brightness = Color.RGBtoHSB(bColor.getRed(),
809 // bColor.getGreen(), bColor.getBlue(), hsbvals)[2];
810 // Color.RGBtoHSB(hsColor.getRed(), hsColor.getGreen(),
811 // hsColor.getBlue(), hsbvals);
812 // hsbvals[2] = (brightness + hsbvals[2])/2.f;
813 // Color result = Color.getHSBColor(hsbvals[0], hsbvals[1], hsbvals[2]);
814 // return result;
815 // }
816
817
818 /**
819 * Gets the listCellRendererComponent attribute of the BlockRenderer
820 * object
821 *
822 * @param list Description of the Parameter
823 * @param value Description of the Parameter
824 * @param index Description of the Parameter
825 * @param isSelected Description of the Parameter
826 * @param cellHasFocus Description of the Parameter
827 * @return The listCellRendererComponent value
828 */
829 public Component getListCellRendererComponent(JList list,
830 Object value, int index, boolean isSelected,
831 boolean cellHasFocus) {
832
833 Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
834 setEnabled(list.isEnabled());
835
836 if (isSelected) {
837 setBackground(list.getSelectionBackground());
838 } else {
839 setBackground(list.getBackground());
840 }
841
842
843 listHeight = list.getSize().height;
844 setText(value.toString());
845
846 if (value instanceof VarBlock) {
847 setFont(list.getFont().deriveFont(Font.ITALIC));
848 // if(isSelected){
849 // if(!list.getSelectionForeground()
850 // .equals(listSelectionForeground)){
851 // Color lsf = list.getSelectionForeground();
852 // filteredSelectionForeground = mixColors(lsf,
853 // Globals.getColor(Globals.COLOR_FILTERED));
854 // unfilteredSelectionForeground = mixColors(lsf,
855 // Globals.getColor(Globals.COLOR_UNFILTERED));
856 // listSelectionForeground = lsf;
857 // }
858 // setForeground(((VarBlock) value).isFiltered()
859 // ? filteredSelectionForeground
860 // : unfilteredSelectionForeground);
861 // } else {
862 // if(!list.getForeground().equals(listForeground)){
863 // Color lf = list.getForeground();
864 // filteredForeground = mixColors(lf,
865 // Globals.getColor(Globals.COLOR_FILTERED));
866 // unfilteredForeground = mixColors(lf,
867 // Globals.getColor(Globals.COLOR_UNFILTERED));
868 // listForeground = lf;
869 // }
870 // setForeground(((VarBlock) value).isFiltered()
871 // ? filteredForeground
872 // : unfilteredForeground);
873 // }
874 setForeground(((VarBlock) value).isFiltered()
875 ? Globals.getColor(Globals.COLOR_FILTERED)
876 : Globals.getColor(Globals.COLOR_UNFILTERED));
877 } else {
878 setForeground((isSelected)
879 ? list.getSelectionForeground()
880 : list.getForeground());
881 setFont(list.getFont().deriveFont(Font.PLAIN));
882 }
883
884 setBorder(CELL_BORDER);
885 return this;
886 }
887 }
888
889
890 /**
891 * Description of the Class
892 *
893 * @author ringler
894 */
895 private class MoveAction extends AbstractAction {
896 private final int actionType;
897
898
899 /**
900 * Constructs a new MoveAction.
901 *
902 * @param which Description of the Parameter
903 */
904 public MoveAction(int which) {
905 super(Globals.getStringArray("MOVE_ACTIONS")[which]);
906 actionType = which;
907 }
908
909
910 /**
911 * Description of the Method
912 *
913 * @param e Description of the Parameter
914 */
915 public void actionPerformed(ActionEvent e) {
916 int from = mouseIndex;
917 int to = mouseIndex;
918 int end = pattern.getSize() - 1;
919 switch (actionType) {
920 case MOVE_LEFT:
921 if (mouseIndex > 0) {
922 to--;
923 }
924 break;
925 case MOVE_RIGHT:
926 if (mouseIndex < end) {
927 to++;
928 }
929 break;
930 case MOVE_START:
931 to = 0;
932 break;
933 case MOVE_END:
934 to = end;
935 break;
936 default:
937 }
938 pattern.move(from, to);
939 patternView.setSelectedIndex(to);
940 patternView.ensureIndexIsVisible(to);
941 }
942 }
943 }