From 9fd13dceae7f28c4eb6f384d658bcba02a319ae9 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 25 Jan 2009 01:27:40 +0000 Subject: [PATCH] * changed static class Factor to proper enum Direction * some cleanup --- .../ui/NotificationLoggingHandler.java | 5 +- .../panel/subtitle/LanguageMatcherEditor.java | 58 -------- .../panel/subtitle/SubtitlePackagePanel.java | 131 +----------------- .../tuned/ui/notification/Direction.java | 33 +++++ .../tuned/ui/notification/Factor.java | 127 ----------------- .../notification/QueueNotificationLayout.java | 61 ++++---- .../SimpleNotificationLayout.java | 27 ++-- 7 files changed, 79 insertions(+), 363 deletions(-) delete mode 100644 source/net/sourceforge/filebot/ui/panel/subtitle/LanguageMatcherEditor.java create mode 100644 source/net/sourceforge/tuned/ui/notification/Direction.java delete mode 100644 source/net/sourceforge/tuned/ui/notification/Factor.java diff --git a/source/net/sourceforge/filebot/ui/NotificationLoggingHandler.java b/source/net/sourceforge/filebot/ui/NotificationLoggingHandler.java index 9fc06699..1aac7598 100644 --- a/source/net/sourceforge/filebot/ui/NotificationLoggingHandler.java +++ b/source/net/sourceforge/filebot/ui/NotificationLoggingHandler.java @@ -3,13 +3,14 @@ package net.sourceforge.filebot.ui; import static net.sourceforge.filebot.Settings.getApplicationName; +import static net.sourceforge.tuned.ui.notification.Direction.NORTH; +import static net.sourceforge.tuned.ui.notification.Direction.SOUTH; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import javax.swing.Icon; -import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import net.sourceforge.filebot.ResourceManager; @@ -25,7 +26,7 @@ public class NotificationLoggingHandler extends Handler { public NotificationLoggingHandler() { - this(new NotificationManager(new QueueNotificationLayout(SwingConstants.NORTH, SwingConstants.SOUTH))); + this(new NotificationManager(new QueueNotificationLayout(NORTH, SOUTH))); } diff --git a/source/net/sourceforge/filebot/ui/panel/subtitle/LanguageMatcherEditor.java b/source/net/sourceforge/filebot/ui/panel/subtitle/LanguageMatcherEditor.java deleted file mode 100644 index 8f803a7d..00000000 --- a/source/net/sourceforge/filebot/ui/panel/subtitle/LanguageMatcherEditor.java +++ /dev/null @@ -1,58 +0,0 @@ - -package net.sourceforge.filebot.ui.panel.subtitle; - - -import ca.odell.glazedlists.EventList; -import ca.odell.glazedlists.event.ListEvent; -import ca.odell.glazedlists.event.ListEventListener; -import ca.odell.glazedlists.matchers.AbstractMatcherEditor; -import ca.odell.glazedlists.matchers.Matcher; - - -public class LanguageMatcherEditor extends AbstractMatcherEditor { - - private final EventList languages; - - - public LanguageMatcherEditor(LanguageSelectionPanel languageSelectionPanel) { - this(languageSelectionPanel.getSelected()); - currentMatcher = new LanguageMatcher(this.languages); - } - - - public LanguageMatcherEditor(EventList languages) { - this.languages = languages; - - languages.addListEventListener(new LanguageSelectionListener()); - } - - - private class LanguageSelectionListener implements ListEventListener { - - @Override - public void listChanged(ListEvent listChanges) { - boolean insert = false; - boolean delete = false; - - while (listChanges.next()) { - int type = listChanges.getType(); - - insert |= (type == ListEvent.INSERT); - delete |= (type == ListEvent.DELETE); - } - - if (insert || delete) { - Matcher matcher = new LanguageMatcher(languages); - - if (insert && !delete) { - fireRelaxed(matcher); - } else if (!insert && delete) { - fireConstrained(matcher); - } else { - fireChanged(matcher); - } - } - } - } - -} diff --git a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePackagePanel.java b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePackagePanel.java index e23c96cd..d0ea07c7 100644 --- a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePackagePanel.java +++ b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePackagePanel.java @@ -13,7 +13,6 @@ import javax.swing.JScrollPane; import ca.odell.glazedlists.BasicEventList; import ca.odell.glazedlists.EventList; -import ca.odell.glazedlists.FilterList; import ca.odell.glazedlists.ObservableElementList; import ca.odell.glazedlists.swing.EventListModel; @@ -22,12 +21,9 @@ public class SubtitlePackagePanel extends JComponent { private final EventList model = new BasicEventList(); - private final LanguageSelectionPanel languageSelection = new LanguageSelectionPanel(model); - public SubtitlePackagePanel() { setLayout(new BorderLayout()); - add(languageSelection, BorderLayout.NORTH); add(new JScrollPane(createList()), BorderLayout.CENTER); } @@ -38,8 +34,7 @@ public class SubtitlePackagePanel extends JComponent { protected JList createList() { - FilterList filterList = new FilterList(model, new LanguageMatcherEditor(languageSelection)); - ObservableElementList observableList = new ObservableElementList(filterList, new SubtitlePackageConnector()); + ObservableElementList observableList = new ObservableElementList(model, new SubtitlePackageConnector()); JList list = new JList(new EventListModel(observableList)); @@ -74,7 +69,7 @@ public class SubtitlePackagePanel extends JComponent { } - private class SubtitlePackageListener implements PropertyChangeListener { + protected class SubtitlePackageListener implements PropertyChangeListener { private final SubtitlePackage subtitlePackage; @@ -91,126 +86,4 @@ public class SubtitlePackagePanel extends JComponent { } - /* - private void updateLanguageFilterButtonPanel() { - - SortedSet languages = new TreeSet(String.CASE_INSENSITIVE_ORDER); - - for (int i = 0; i < unfilteredModel.getSize(); i++) { - SubtitlePackage subtitle = (SubtitlePackage) unfilteredModel.getElementAt(i); - languages.add(subtitle.getLanguageName()); - } - - languageFilterPanel.removeAll(); - - for (String language : languages) { - LanguageFilterButton languageFilterButton = createLanguageFilterButton(language); - languageFilterButton.addItemListener(new LanguageFilterItemListener(language)); - - languageFilterPanel.add(languageFilterButton); - } - } - - - private void updateFilteredModel() { - SimpleListModel model = new SimpleListModel(); - - for (int i = 0; i < unfilteredModel.getSize(); i++) { - SubtitlePackage subtitle = (SubtitlePackage) unfilteredModel.getElementAt(i); - - if (isLanguageSelected(subtitle.getLanguageName())) { - model.add(subtitle); - } - } - - super.setModel(model); - } - - - public boolean isLanguageSelected(String language) { - return !languageFilterSelection.containsKey(language) || languageFilterSelection.get(language); - } - - - public void setLanguageSelected(String language, boolean selected) { - languageFilterSelection.put(language, selected); - - Settings.getSettings().asBooleanMap(Settings.SUBTITLE_LANGUAGE).put(language, selected); - } - - - private LanguageFilterButton createLanguageFilterButton(String language) { - Locale locale = LanguageResolver.getDefault().getLocale(language); - - boolean selected = isLanguageSelected(language); - - if (locale != null) - return new LanguageFilterButton(locale, selected); - else - return new LanguageFilterButton(language, selected); - } - - - private class LanguageFilterItemListener implements ItemListener { - - private final String language; - - - public LanguageFilterItemListener(String language) { - this.language = language; - } - - - @Override - public void itemStateChanged(ItemEvent e) { - boolean selected = (e.getStateChange() == ItemEvent.SELECTED); - - setLanguageSelected(language, selected); - - updateFilteredModel(); - } - - }; - - - private class LanguageFilterButton extends JToggleButton { - - public LanguageFilterButton(Locale locale, boolean selected) { - this(locale.getDisplayLanguage(Locale.ENGLISH), ResourceManager.getFlagIcon(locale.getLanguage()), selected); - } - - - public LanguageFilterButton(String language, boolean selected) { - this(language, ResourceManager.getFlagIcon(null), selected); - } - - - public LanguageFilterButton(String language, Icon icon, boolean selected) { - super(icon, selected); - - setToolTipText(language); - setContentAreaFilled(false); - setFocusPainted(false); - - setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); - - setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight())); - } - - - @Override - protected void paintComponent(Graphics g) { - Graphics2D g2d = (Graphics2D) g; - - // make transparent if not selected - if (!isSelected()) { - AlphaComposite composite = AlphaComposite.SrcOver.derive(0.2f); - g2d.setComposite(composite); - } - - super.paintComponent(g2d); - } - - } - */ } diff --git a/source/net/sourceforge/tuned/ui/notification/Direction.java b/source/net/sourceforge/tuned/ui/notification/Direction.java new file mode 100644 index 00000000..ca214933 --- /dev/null +++ b/source/net/sourceforge/tuned/ui/notification/Direction.java @@ -0,0 +1,33 @@ + +package net.sourceforge.tuned.ui.notification; + + +import javax.swing.SwingConstants; + + +public enum Direction { + CENTER(0, 0, 0.5, 0.5, SwingConstants.CENTER), + NORTH(0, -1, 0.5, 0.0, SwingConstants.NORTH), + NORTH_EAST(1, -1, 1.0, 0.0, SwingConstants.NORTH_EAST), + EAST(1, 0, 1.0, 0.5, SwingConstants.EAST), + SOUTH_EAST(1, 1, 1.0, 1.0, SwingConstants.SOUTH_EAST), + SOUTH(0, 1, 0.5, 1.0, SwingConstants.SOUTH), + SOUTH_WEST(-1, 1, 0.0, 1.0, SwingConstants.SOUTH_WEST), + WEST(-1, 0, 0.0, 0.5, SwingConstants.WEST), + NORTH_WEST(-1, -1, 0.0, 0.0, SwingConstants.NORTH_WEST); + + public final int vx; + public final int vy; + public final double ax; + public final double ay; + public final int swingConstant; + + + private Direction(int vx, int vy, double ax, double ay, int swingConstant) { + this.vx = vx; + this.vy = vy; + this.ax = ax; + this.ay = ay; + this.swingConstant = swingConstant; + } +} diff --git a/source/net/sourceforge/tuned/ui/notification/Factor.java b/source/net/sourceforge/tuned/ui/notification/Factor.java deleted file mode 100644 index 67f09a2a..00000000 --- a/source/net/sourceforge/tuned/ui/notification/Factor.java +++ /dev/null @@ -1,127 +0,0 @@ - -package net.sourceforge.tuned.ui.notification; - - -import javax.swing.SwingConstants; - - -class Factor implements SwingConstants { - - public final double fx; - public final double fy; - - - public Factor(double fx, double fy) { - this.fx = fx; - this.fy = fy; - } - - - static Factor getOrientationFactor(int orientation) { - double fx = 0; - double fy = 0; - - switch (orientation) { - case NORTH_WEST: - fx = 0; - fy = 0; - break; - - case NORTH: - fx = 0.5; - fy = 0; - break; - - case NORTH_EAST: - fx = 1; - fy = 0; - break; - - case WEST: - fx = 0; - fy = 0.5; - break; - - case EAST: - fx = 1; - fy = 0.5; - break; - - case SOUTH_WEST: - fx = 0; - fy = 1; - break; - - case SOUTH: - fx = 0.5; - fy = 1; - break; - - case SOUTH_EAST: - fx = 1; - fy = 1; - break; - - case CENTER: - fx = 0.5; - fy = 0.5; - } - - return new Factor(fx, fy); - } - - - static Factor getDirectionFactor(int direction) { - double fx = 0; - double fy = 0; - - switch (direction) { - case NORTH_WEST: - fx = -1; - fy = -1; - break; - - case NORTH: - fx = 0; - fy = -1; - break; - - case NORTH_EAST: - fx = 1; - fy = -1; - break; - - case WEST: - fx = -1; - fy = 0; - break; - - case EAST: - fx = 1; - fy = 0; - break; - - case SOUTH_WEST: - fx = -1; - fy = 1; - break; - - case SOUTH: - fx = 0; - fy = 1; - break; - - case SOUTH_EAST: - fx = 1; - fy = 1; - break; - - case CENTER: - fx = 0; - fy = 0; - } - - return new Factor(fx, fy); - } - -} diff --git a/source/net/sourceforge/tuned/ui/notification/QueueNotificationLayout.java b/source/net/sourceforge/tuned/ui/notification/QueueNotificationLayout.java index ba1ea22a..66640bfd 100644 --- a/source/net/sourceforge/tuned/ui/notification/QueueNotificationLayout.java +++ b/source/net/sourceforge/tuned/ui/notification/QueueNotificationLayout.java @@ -6,24 +6,25 @@ package net.sourceforge.tuned.ui.notification; +import static net.sourceforge.tuned.ui.notification.Direction.SOUTH_EAST; +import static net.sourceforge.tuned.ui.notification.Direction.WEST; + import java.awt.Dimension; import java.awt.GraphicsConfiguration; import java.awt.Insets; import java.awt.Point; import java.awt.Toolkit; import java.util.ArrayList; -import java.util.ListIterator; - -import javax.swing.SwingConstants; +import java.util.List; -public class QueueNotificationLayout implements NotificationLayout, SwingConstants { +public class QueueNotificationLayout implements NotificationLayout { - private ArrayList notificationList = new ArrayList(); + private final List notifications = new ArrayList(); - private int orientation; - private int direction; - private int growAnchor; + private final Direction alignment; + private final Direction direction; + private final Direction growAnchor; public QueueNotificationLayout() { @@ -31,30 +32,28 @@ public class QueueNotificationLayout implements NotificationLayout, SwingConstan } - public QueueNotificationLayout(int orientation, int direction) { - this.orientation = orientation; - this.growAnchor = orientation; + public QueueNotificationLayout(Direction alignment, Direction direction) { + this.alignment = alignment; + this.growAnchor = alignment; this.direction = direction; } - public QueueNotificationLayout(int orientation, int direction, int growAnchor) { - this.orientation = orientation; + public QueueNotificationLayout(Direction orientation, Direction direction, Direction growAnchor) { + this.alignment = orientation; this.direction = direction; this.growAnchor = growAnchor; } private Point getBaseAnchor(Dimension screen, Insets insets) { - Factor f = Factor.getOrientationFactor(orientation); - Point p = new Point(); screen.height -= insets.top + insets.bottom; screen.width -= insets.left + insets.right; - p.x = (int) (f.fx * screen.width); - p.y = (int) (f.fy * screen.height); + p.x = (int) (alignment.ax * screen.width); + p.y = (int) (alignment.ay * screen.height); p.x += insets.left; p.y += insets.top; @@ -64,28 +63,27 @@ public class QueueNotificationLayout implements NotificationLayout, SwingConstan private Point getLocation(Point anchor, Dimension size) { - Factor f = Factor.getOrientationFactor(growAnchor); - Point p = new Point(); - p.x = (int) (anchor.x - size.width * f.fx); - p.y = (int) (anchor.y - size.height * f.fy); + + p.x = (int) (anchor.x - size.width * growAnchor.ax); + p.y = (int) (anchor.y - size.height * growAnchor.ay); return p; } private Point getNextAnchor(Point anchor, Dimension size) { - Factor f = Factor.getDirectionFactor(direction); - Point p = new Point(); - p.x = (int) (anchor.x + size.width * f.fx); - p.y = (int) (anchor.y + size.height * f.fy); + + p.x = anchor.x + size.width * direction.vx; + p.y = anchor.y + size.height * direction.vy; + return p; } public void add(NotificationWindow notification) { - notificationList.add(notification); + notifications.add(notification); align(notification.getGraphicsConfiguration()); } @@ -96,14 +94,11 @@ public class QueueNotificationLayout implements NotificationLayout, SwingConstan Point anchor = getBaseAnchor(screen, insets); - ListIterator i = notificationList.listIterator(); - - while (i.hasNext()) { - NotificationWindow n = i.next(); - Dimension size = n.getSize(); + for (NotificationWindow window : notifications) { + Dimension size = window.getSize(); Point p = getLocation(anchor, size); - n.setLocation(p); + window.setLocation(p); anchor = getNextAnchor(anchor, size); } @@ -111,7 +106,7 @@ public class QueueNotificationLayout implements NotificationLayout, SwingConstan public void remove(NotificationWindow notification) { - if (notificationList.remove(notification)) { + if (notifications.remove(notification)) { align(notification.getGraphicsConfiguration()); } } diff --git a/source/net/sourceforge/tuned/ui/notification/SimpleNotificationLayout.java b/source/net/sourceforge/tuned/ui/notification/SimpleNotificationLayout.java index c7d5b6ef..1067c621 100644 --- a/source/net/sourceforge/tuned/ui/notification/SimpleNotificationLayout.java +++ b/source/net/sourceforge/tuned/ui/notification/SimpleNotificationLayout.java @@ -6,17 +6,18 @@ package net.sourceforge.tuned.ui.notification; +import static net.sourceforge.tuned.ui.notification.Direction.NORTH; + import java.awt.Dimension; import java.awt.Insets; import java.awt.Point; import java.awt.Toolkit; -import javax.swing.SwingConstants; -public class SimpleNotificationLayout implements NotificationLayout, SwingConstants { +public class SimpleNotificationLayout implements NotificationLayout { private NotificationWindow currentNotification; - private int orientation; + private Direction alignment; public SimpleNotificationLayout() { @@ -24,21 +25,19 @@ public class SimpleNotificationLayout implements NotificationLayout, SwingConsta } - public SimpleNotificationLayout(int orientation) { - this.orientation = orientation; + public SimpleNotificationLayout(Direction alignment) { + this.alignment = alignment; } private Point getBaseAnchor(Dimension screen, Insets insets) { - Factor f = Factor.getOrientationFactor(orientation); - Point p = new Point(); screen.height -= insets.top + insets.bottom; screen.width -= insets.left + insets.right; - p.x = (int) (f.fx * screen.width); - p.y = (int) (f.fy * screen.height); + p.x = (int) (alignment.ax * screen.width); + p.y = (int) (alignment.ay * screen.height); p.x += insets.left; p.y += insets.top; @@ -48,11 +47,10 @@ public class SimpleNotificationLayout implements NotificationLayout, SwingConsta private Point getLocation(Point anchor, Dimension size) { - Factor f = Factor.getOrientationFactor(orientation); - Point p = new Point(); - p.x = (int) (anchor.x - size.width * f.fx); - p.y = (int) (anchor.y - size.height * f.fy); + + p.x = (int) (anchor.x - size.width * alignment.ax); + p.y = (int) (anchor.y - size.height * alignment.ay); return p; } @@ -66,8 +64,9 @@ public class SimpleNotificationLayout implements NotificationLayout, SwingConsta Point anchor = getBaseAnchor(screen, insets); notification.setLocation(getLocation(anchor, size)); - if (currentNotification != null) + if (currentNotification != null) { currentNotification.close(); + } currentNotification = notification; }