From c0ad7812cd0caf7096a7b431a1da397de1151575 Mon Sep 17 00:00:00 2001 From: North Date: Mon, 24 Dec 2012 18:43:54 +0200 Subject: [PATCH] minor changes (mostly code style) --- .../src/main/java/mage/client/MageFrame.java | 72 +- .../main/java/mage/client/cards/BigCard.java | 96 +- .../mage/client/components/HoverButton.java | 24 +- .../components/layout/RelativeLayout.java | 1577 ++++++++--------- .../collection/viewer/MageBook.java | 26 +- .../java/mage/client/table/TablesPane.java | 11 +- .../java/mage/client/table/TablesPanel.java | 8 +- .../java/org/mage/card/arcane/Animation.java | 124 +- .../java/org/mage/card/arcane/CardPanel.java | 137 +- .../org/mage/card/arcane/ManaSymbols.java | 29 +- .../mage/card/arcane/ScaledImagePanel.java | 37 +- .../main/java/org/mage/card/arcane/UI.java | 82 +- .../main/java/org/mage/card/arcane/Util.java | 19 +- .../org/mage/plugins/card/CardPluginImpl.java | 124 +- Mage.Common/src/mage/cards/MageCard.java | 44 +- Mage.Common/src/mage/cards/MagePermanent.java | 6 +- Mage.Common/src/mage/utils/ThreadUtils.java | 12 +- .../mage/server/console/ConsolePanel.java | 6 +- .../mage/sets/darkascension/Immerwolf.java | 6 +- .../src/mage/sets/eventide/BalefireLiege.java | 8 +- .../mage/sets/eventide/DeathbringerLiege.java | 8 +- .../mage/sets/guildpact/TiborAndLumia.java | 6 +- 22 files changed, 1170 insertions(+), 1292 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/MageFrame.java b/Mage.Client/src/main/java/mage/client/MageFrame.java index 6edf39daeb..863ea0b882 100644 --- a/Mage.Client/src/main/java/mage/client/MageFrame.java +++ b/Mage.Client/src/main/java/mage/client/MageFrame.java @@ -111,9 +111,9 @@ import java.util.prefs.Preferences; */ public class MageFrame extends javax.swing.JFrame implements MageClient { - private final static Logger logger = Logger.getLogger(MageFrame.class); - private final static String liteModeArg = "-lite"; - private final static String grayModeArg = "-gray"; + private static final Logger logger = Logger.getLogger(MageFrame.class); + private static final String liteModeArg = "-lite"; + private static final String grayModeArg = "-gray"; private static MageFrame instance; @@ -124,7 +124,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { private static Preferences prefs = Preferences.userNodeForPackage(MageFrame.class); private JLabel title; private Rectangle titleRectangle; - private final static MageVersion version = new MageVersion(0, 10, 0, "dev"); + private static final MageVersion version = new MageVersion(0, 10, 0, "dev"); private UUID clientId; private static MagePane activeFrame; private static boolean liteMode = false; @@ -196,14 +196,11 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { try { UIManager.put("desktop", new Color(0, 0, 0, 0)); UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); - //MageSynthStyleFactory f = new MageSynthStyleFactory(SynthLookAndFeel.getStyleFactory()); - //SynthLookAndFeel.setStyleFactory(f); } catch (Exception ex) { logger.fatal(null, ex); } ManaSymbols.loadImages(); - //ManaSymbols.loadImages(); Plugins.getInstance().loadPlugins(); initComponents(); @@ -253,11 +250,6 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { setAppIcon(); MageTray.getInstance().install(); - //PlayerPanelNew n = new PlayerPanelNew(); - //n.setBounds(100,100,100,300); - //n.setVisible(true); - //backgroundPane.add(n); - desktopPane.add(ArrowBuilder.getArrowsPanel(), JLayeredPane.DRAG_LAYER); desktopPane.addComponentListener(new ComponentAdapter() { @@ -270,9 +262,10 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { backgroundPane.setSize(width, height); } JPanel arrowsPanel = ArrowBuilder.getArrowsPanelRef(); - if (arrowsPanel != null) arrowsPanel.setSize(width, height); + if (arrowsPanel != null) { + arrowsPanel.setSize(width, height); + } if (title != null) { - //title.setBorder(BorderFactory.createLineBorder(Color.red)); title.setBounds((int) (width - titleRectangle.getWidth()) / 2, (int) (height - titleRectangle.getHeight()) / 2, titleRectangle.width, titleRectangle.height); } } @@ -292,6 +285,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { btnDownloadSymbols.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); btnDownloadSymbols.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); btnDownloadSymbols.addActionListener(new java.awt.event.ActionListener() { + @Override public void actionPerformed(java.awt.event.ActionEvent evt) { btnSymbolsActionPerformed(evt); } @@ -307,6 +301,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { btnDownload.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); btnDownload.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); btnDownload.addActionListener(new java.awt.event.ActionListener() { + @Override public void actionPerformed(java.awt.event.ActionEvent evt) { btnImagesActionPerformed(evt); } @@ -326,14 +321,16 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { ui.addButton(MageComponents.TABLES_MENU_BUTTON, btnGames); SwingUtilities.invokeLater(new Runnable() { + @Override public void run() { disableButtons(); - if (PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_CHECK, "true").equals("true")) + if (PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_CHECK, "true").equals("true")) { checkForNewImages(); + } logger.info("Client start up time: " + ((System.currentTimeMillis() - startTime) / 1000 + " seconds")); - if (autoConnect()) + if (autoConnect()) { enableButtons(); - else { + } else { connectDialog.showDialog(); } } @@ -380,13 +377,13 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { } private void setBackground() { - if (liteMode || grayMode) + if (liteMode || grayMode) { return; + } String filename = "/background.jpg"; try { if (Plugins.getInstance().isThemePluginLoaded()) { - Map ui = new HashMap(); - backgroundPane = (ImagePanel) Plugins.getInstance().updateTablePanel(ui); + backgroundPane = (ImagePanel) Plugins.getInstance().updateTablePanel(new HashMap()); } else { InputStream is = this.getClass().getResourceAsStream(filename); BufferedImage background = ImageIO.read(is); @@ -400,8 +397,9 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { } private void addMageLabel() { - if (liteMode || grayMode) + if (liteMode || grayMode) { return; + } String filename = "/label-mage.png"; try { InputStream is = this.getClass().getResourceAsStream(filename); @@ -429,6 +427,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { private AbstractButton createWindowsButton() { final JToggleButton windowButton = new JToggleButton("Windows"); windowButton.addItemListener(new ItemListener() { + @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { createAndShowMenu((JComponent) e.getSource(), windowButton); @@ -465,13 +464,16 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { } menu.addPopupMenuListener(new PopupMenuListener() { + @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) { } + @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { windowButton.setSelected(false); } + @Override public void popupMenuCanceled(PopupMenuEvent e) { windowButton.setSelected(false); } @@ -519,11 +521,13 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { } public static void setActive(MagePane frame) { - if (frame == null) + if (frame == null) { return; + } logger.debug("Setting " + frame.getTitle() + " active"); - if (activeFrame != null) + if (activeFrame != null) { activeFrame.deactivated(); + } activeFrame = frame; activeFrame.setVisible(true); activeFrame.toFront(); @@ -538,8 +542,9 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { public static void deactivate(MagePane frame) { frame.setVisible(false); MagePane topmost = getTopMost(frame); - if (activeFrame != frame) + if (activeFrame != frame) { frame.deactivated(); + } setActive(topmost); } @@ -930,20 +935,19 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { public void showDeckEditor(DeckEditorMode mode, Deck deck, UUID tableId, int time) { String name; - if (mode == DeckEditorMode.Sideboard || mode == DeckEditorMode.Limited) + if (mode == DeckEditorMode.Sideboard || mode == DeckEditorMode.Limited) { name = "Deck Editor - " + tableId.toString(); - else { - if (deck != null) + } else { + if (deck != null) { name = "Deck Editor - " + deck.getName(); - else + } else { name = "Deck Editor"; + } JInternalFrame[] windows = desktopPane.getAllFramesInLayer(JLayeredPane.DEFAULT_LAYER); for (JInternalFrame window : windows) { - if (window instanceof DeckEditorPane) { - if (window.getTitle().equals(name)) { - setActive((MagePane)window); - return; - } + if (window instanceof DeckEditorPane && window.getTitle().equals(name)) { + setActive((MagePane) window); + return; } } } @@ -994,11 +998,13 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { startTime = System.currentTimeMillis(); Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + @Override public void uncaughtException(Thread t, Throwable e) { logger.fatal(null, e); } }); SwingUtilities.invokeLater(new Runnable() { + @Override public void run() { CardScanner.scan(); for (String arg : args) { diff --git a/Mage.Client/src/main/java/mage/client/cards/BigCard.java b/Mage.Client/src/main/java/mage/client/cards/BigCard.java index 0f1344f5cd..dcbbf41e9d 100644 --- a/Mage.Client/src/main/java/mage/client/cards/BigCard.java +++ b/Mage.Client/src/main/java/mage/client/cards/BigCard.java @@ -66,10 +66,6 @@ public class BigCard extends JComponent { protected float hue = 0.005f; protected float dh = 0.005f; - static private final int DEFAULT_DELAY_PERIOD = 30; - static private final float LEFT_BOUNDARY = 0f; - static private final float RIGHT_BOUNDARY = 0.5f; - public BigCard() { initComponents(); if (!Plugins.getInstance().isCardPluginLoaded()) { @@ -81,7 +77,7 @@ public class BigCard extends JComponent { this.scrollPane.setVisible(false); } - protected void initBounds() { + private void initBounds() { oldWidth = this.getWidth(); scrollPane.setBounds(this.getWidth()*1000/17777,this.getWidth()*1000/1100, this.getWidth()*1000/1142,this.getWidth()*1000/2539); @@ -89,7 +85,9 @@ public class BigCard extends JComponent { public void setCard(UUID cardId, Image image, List strings, boolean foil) { if (this.cardId == null || !this.cardId.equals(cardId)) { - if (this.panel != null) remove(this.panel); + if (this.panel != null) { + remove(this.panel); + } this.cardId = cardId; bigImage = image; synchronized (this) { @@ -124,20 +122,6 @@ public class BigCard extends JComponent { @Override public void paintComponent(Graphics graphics) { - - /*if (foilState) { - if (source != null) { - synchronized (BigCard.class) { - if (source != null) { - graphics.drawImage(foil, 0, 0, this); - } - } - } - } else { - if (bigImage != null) { - graphics.drawImage(bigImage, 0, 0, this); - } - }*/ if (bigImage != null) { graphics.drawImage(bigImage, 0, 0, this); } @@ -156,81 +140,9 @@ public class BigCard extends JComponent { } public void setFoil(boolean foil) { - /*if (foilThread == null) { - synchronized (this) { - if (foilThread == null) { - foilThread = getFoilThread(); - foilThread.setDaemon(true); - foilThread.start(); - } - } - } - if (foil != foilState) { - synchronized (this) { - if (foil != foilState) { - hue = 0.005f; - foilState = foil; - } - } - } - */ repaint(); } -// private Thread getFoilThread() { -// return new Thread(new Runnable() { -// @Override -// public void run() { -// if (bigImage == null) { -// return; -// } -// final HueFilter filter = FilterFactory.getHueFilter(); -// int sign = 1; -// while (true) { -// boolean prevState = foilState; -// while (!foilState) { -// ThreadUtils.sleep(10); -// } -// if (prevState == foilState) { -// ThreadUtils.sleep(DEFAULT_DELAY_PERIOD); -// } -// hue += dh * sign; -// /*if (hue >= 1.0D) { -// hue = 0.000F; -// }*/ -// if (hue < LEFT_BOUNDARY) { -// sign *= -1; -// hue = LEFT_BOUNDARY; -// } else if (hue > RIGHT_BOUNDARY) { -// sign *= -1; -// hue = RIGHT_BOUNDARY; -// } -// filter.setHue(hue); -// BufferedImage f = null; -// synchronized (BigCard.this) { -// if (source == null) { -// source = BufferedImageBuilder.bufferImage(bigImage); -// } -// if (source == null) { -// return; -// } -// f = filter.filter(source, null); -// } -// synchronized (BigCard.class) { -// foil = f; -// } -// SwingUtilities.invokeLater(new Runnable() { -// @Override -// public void run() { -// BigCard.this.repaint(); -// } -// }); -// -// } -// } -// }); -// } - public void addJXPanel(UUID cardId, JXPanel jxPanel) { bigImage = null; synchronized (this) { diff --git a/Mage.Client/src/main/java/mage/client/components/HoverButton.java b/Mage.Client/src/main/java/mage/client/components/HoverButton.java index 1fa6300612..cfa1370a96 100644 --- a/Mage.Client/src/main/java/mage/client/components/HoverButton.java +++ b/Mage.Client/src/main/java/mage/client/components/HoverButton.java @@ -36,16 +36,17 @@ public class HoverButton extends JPanel implements MouseListener { private Command onHover = null; private Color textColor = Color.white; - final static Font textFont = new Font("Arial", Font.PLAIN, 12); - final static Font textFontMini = new Font("Arial", Font.PLAIN, 11); - final static Font textSetFontBoldMini = new Font("Arial", Font.BOLD, 12); - final static Font textSetFontBold = new Font("Arial", Font.BOLD, 14); + static final Font textFont = new Font("Arial", Font.PLAIN, 12); + static final Font textFontMini = new Font("Arial", Font.PLAIN, 11); + static final Font textSetFontBoldMini = new Font("Arial", Font.BOLD, 12); + static final Font textSetFontBold = new Font("Arial", Font.BOLD, 14); private boolean useMiniFont = false; public HoverButton(String text, Image image, Rectangle size) { this(text, image, image, null, image, size); - if (image == null) + if (image == null) { throw new IllegalArgumentException("Image can't be null"); + } } public HoverButton(String text, Image image, Image hover, Image disabled, Rectangle size) { @@ -74,9 +75,14 @@ public class HoverButton extends JPanel implements MouseListener { if (isHovered) { g.drawImage(hoverImage, 0, 0, imageSize.width, imageSize.height, this); if (text != null) { - if (textColor != null) g2d.setColor(textColor); - if (useMiniFont) g2d.setFont(textFontMini); - else g2d.setFont(textFont); + if (textColor != null) { + g2d.setColor(textColor); + } + if (useMiniFont) { + g2d.setFont(textFontMini); + } else { + g2d.setFont(textFont); + } textOffsetX = calculateOffset(g2d); g2d.drawString(text, textOffsetX, textOffsetY); } @@ -124,7 +130,7 @@ public class HoverButton extends JPanel implements MouseListener { frc = g2d.getFontRenderContext(); textWidth = (int) textFontMini.getStringBounds(text, frc).getWidth(); } - textOffsetX = (int) ((imageSize.width - textWidth) / 2); + textOffsetX = (imageSize.width - textWidth) / 2; } return textOffsetX; } diff --git a/Mage.Client/src/main/java/mage/client/components/layout/RelativeLayout.java b/Mage.Client/src/main/java/mage/client/components/layout/RelativeLayout.java index 5c20009665..2e8be5b2b6 100644 --- a/Mage.Client/src/main/java/mage/client/components/layout/RelativeLayout.java +++ b/Mage.Client/src/main/java/mage/client/components/layout/RelativeLayout.java @@ -1,13 +1,18 @@ package mage.client.components.layout; -import java.awt.*; -import java.util.*; +import java.awt.Component; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.Insets; +import java.awt.LayoutManager2; +import java.util.HashMap; +import java.util.Map; /** * The RelativeLayout class is a layout manager that * lays out a container's components on the specified X or Y axis. * - * Components can be layed out at their preferred size or at a + * Components can be layed out at their preferred size or at a * relative size. When relative sizing is used the component must be added * to the container using a relative size constraint, which is simply a * Float value. @@ -29,828 +34,764 @@ import java.util.*; * By defaults components are center aligned on the secondary axis * however this can be changed at the container or component level. */ -public class RelativeLayout implements LayoutManager2, java.io.Serializable -{ - // Used in the constructor - public final static int X_AXIS = 0; - public final static int Y_AXIS = 1; +public class RelativeLayout implements LayoutManager2, java.io.Serializable { - // See setAlignment() method - public final static float LEADING = 0.0f; - public final static float CENTER = 0.5f; - public final static float TRAILING = 1.0f; - public final static float COMPONENT = -1.0f; + private static final long serialVersionUID = 1L; + // Used in the constructor + public final static int X_AXIS = 0; + public final static int Y_AXIS = 1; - // See setRoundingPolicy() method - public final static int DO_NOTHING = 0; - public final static int FIRST = 1; - public final static int LAST = 2; - public final static int LARGEST = 3; - public final static int EQUAL = 4; + // See setAlignment() method + public final static float LEADING = 0.0f; + public final static float CENTER = 0.5f; + public final static float TRAILING = 1.0f; + public final static float COMPONENT = -1.0f; - private final static int MINIMUM = 0; - private final static int PREFERRED = 1; + // See setRoundingPolicy() method + public final static int DO_NOTHING = 0; + public final static int FIRST = 1; + public final static int LAST = 2; + public final static int LARGEST = 3; + public final static int EQUAL = 4; - private HashMap constraints = new HashMap(); + private final static int MINIMUM = 0; + private final static int PREFERRED = 1; - /** - * The axis of the Components within the Container. - */ - private int axis; + private Map constraints = new HashMap(); - /** - * The alignment of the Components on the other axis of the Container. - * For X-AXIS this would refer to the Y alignemt. - * For Y-AXIS this would refer to the X alignment. - */ - private float alignment = CENTER; - - /** - * This is the gap (in pixels) which specifies the space between components - * It can be changed at any time and should be a non-negative integer. - */ - private int gap; - - /** - * The gap (in pixels) used before the leading component and after the - * trailing component. - * It can be changed at any time and should be a non-negative integer. - */ - private int borderGap; - - // Fill space available for relative components - private boolean fill = false; - - // Gap to prevent the component from completely filling the space available - private int fillGap; - - // Specify the rounding policy when rounding problems happen. - private int roundingPolicy = LARGEST; - - /** - * Creates a relative layout with the components layed out on the X-Axis - * using the default gap - */ - public RelativeLayout() - { - this(X_AXIS, 0); - } - - /** - * Creates a relative layout with the components layed out on the specified - * axis using the default gap - *

- * @param axis X-AXIS or Y_AXIS - */ - public RelativeLayout(int axis) - { - this(axis, 0); - } - - /** - * Creates a relative layout with the components layed out on the specified - * axis using the specfied gap - *

- * All RelativeLayout constructors defer to this one. - * @param axis X-AXIS or Y_AXIS - * @param gap the gap - */ - public RelativeLayout(int axis, int gap) - { - setAxis( axis ); - setGap( gap ); - setBorderGap( gap ); - } - - /** - * Gets the layout axis. - * @return the layout axis - */ - public int getAxis() - { - return axis; - } - - /** - * Sets the layout axis - * @param axis the layout axis - */ - public void setAxis(int axis) - { - if (axis != X_AXIS - && axis != Y_AXIS) - throw new IllegalArgumentException("invalid axis specified"); - - this.axis = axis; - } - - /** - * Gets the gap between components. - * @return the gap between components - */ - public int getGap() - { - return gap; - } - - /** - * Sets the gap between components to the specified value. - * @param gap the gap between components - */ - public void setGap(int gap) - { - this.gap = gap < 0 ? 0 : gap; - } - - /** - * Gets the initial gap. This gap is used before the leading component - * and after the trailing component. - * - * @return the leading/trailing gap - */ - public int getBorderGap() - { - return borderGap; - } - - /** - * Sets the initial gap. This gap is used before the leading component - * and after the trailing component. The default is set to the gap. - * - * @param borderGap the leading/trailing gap - */ - public void setBorderGap(int borderGap) - { - this.borderGap = borderGap < 0 ? 0 : borderGap; - } - - /** - * Gets the alignment of the components on the opposite axis. - * @return the alignment - */ - public float getAlignment() - { - return alignment; - } - - /* - * Set the alignment of the component on the opposite axis. - * - * For X-AXIS this would refer to the Y alignemt. - * For Y-AXIS this would refer to the X alignment. - * - * Must be between 0.0 and 1.0, or -1. Values can be specified using: - * - * RelativeLayout.LEADING - * RelativeLayout.CENTER - * RelativeLayout.TRAILING - * RelativeLayout.COMPONENT - the getAlignemntX/Y method for the - * opposite axis will be used - */ - public void setAlignment(float alignment) - { - this.alignment = alignment > 1.0f ? 1.0f : alignment < 0.0f ? -1.0f : alignment; - } - - /** - * Gets the fill property for the component size on the opposite edge. - * @return the fill property - */ - public boolean isFill() - { - return fill; - } - - /** - * Change size of relative components to fill the space available - * For X-AXIS aligned components the height will be filled. - * For Y-AXIS aligned components the width will be filled. - */ - public void setFill(boolean fill) - { - this.fill = fill; - } - - /** - * Gets the fill gap amount. - * @return the fill gap value - */ - public int getFillGap() - { - return fillGap; - } - - /** - * Specify the number of pixels by which the fill size is decreased when - * setFill(true) has been specified. - */ - public void setFillGap(int fillGap) - { - this.fillGap = fillGap; - } - - /** - * Gets the rounding policy. - * @return the rounding policy - */ - public int getRoundingPolicy() - { - return roundingPolicy; - } - - /** - * Specify the rounding policy to be used when all the avialable pixels - * have not been allocated to a component. - * - * DO_NOTHING - * FIRST - extra pixels added to the first relative component - * LAST - extra pixels added to the last relative component - * LARGEST (default) - extra pixels added to the larger relative component - * EQUAL - a single pixel is added to each relative component - * (until pixels are used up) - */ - public void setRoundingPolicy(int roundingPolicy) - { - this.roundingPolicy = roundingPolicy; - } - - /** - * Gets the constraints for the specified component. - * - * @param component the component to be queried - * @return the constraint for the specified component, or null - * if component is null or is not present in this layout + /** + * The axis of the Components within the Container. */ - public Float getConstraints(Component component) - { - return (Float)constraints.get(component); + private int axis; + + /** + * The alignment of the Components on the other axis of the Container. + * For X-AXIS this would refer to the Y alignment. + * For Y-AXIS this would refer to the X alignment. + */ + private float alignment = CENTER; + + /** + * This is the gap (in pixels) which specifies the space between components + * It can be changed at any time and should be a non-negative integer. + */ + private int gap; + + /** + * The gap (in pixels) used before the leading component and after the trailing component. + * It can be changed at any time and should be a non-negative integer. + */ + private int borderGap; + + // Fill space available for relative components + private boolean fill = false; + + // Gap to prevent the component from completely filling the space available + private int fillGap; + + // Specify the rounding policy when rounding problems happen. + private int roundingPolicy = LARGEST; + + /** + * Creates a relative layout with the components layed out on the X-Axis using the default gap + */ + public RelativeLayout() { + this(X_AXIS, 0); } - /** - * Not supported - */ - public void addLayoutComponent(String name, Component component) {} - - /* - * Keep track of any specified constraint for the component. - */ - public void addLayoutComponent(Component component, Object constraint) - { - if (constraint != null) - if (constraint instanceof Float) - { - constraints.put(component, (Float)constraint); - } - else - throw new IllegalArgumentException("Constraint parameter must be of type Float"); - } - - /** - * Removes the specified component from the layout. - * @param comp the component to be removed - */ - public void removeLayoutComponent(Component comp) {} - - /** - * Determines the preferred size of the container argument using - * this column layout. - *

- * The preferred width of a column layout is the largest preferred - * width of each column in the container, plus the horizontal padding - * times the number of columns minus one, - * plus the left and right insets of the target container. - *

- * The preferred height of a column layout is the largest preferred - * height of each row in the container, plus the vertical padding - * times the number of rows minus one, - * plus the top and bottom insets of the target container. - * - * @param target the container in which to do the layout - * @return the preferred dimensions to lay out the - * subcomponents of the specified container - * @see java.awt.RelativeLayout#minimumLayoutSize - * @see java.awt.Container#getPreferredSize() - */ - public Dimension preferredLayoutSize(Container parent) - { - synchronized (parent.getTreeLock()) - { - return getLayoutSize(parent, PREFERRED); - } - } - - /** - * Determines the minimum size of the container argument using this - * column layout. - *

- * The minimum width of a grid layout is the largest minimum width - * of each column in the container, plus the horizontal padding - * times the number of columns minus one, - * plus the left and right insets of the target container. - *

- * The minimum height of a column layout is the largest minimum height - * of each row in the container, plus the vertical padding - * times the number of rows minus one, - * plus the top and bottom insets of the target container. - * - * @param target the container in which to do the layout - * @return the minimum dimensions needed to lay out the - * subcomponents of the specified container - * @see java.awt.RelativeLayout#preferredLayoutSize - * @see java.awt.Container#doLayout - */ - public Dimension minimumLayoutSize(Container parent) - { - synchronized (parent.getTreeLock()) - { - return getLayoutSize(parent, MINIMUM); - } - } - - /** - * Lays out the specified container using this layout. - *

- * This method reshapes the components in the specified target - * container in order to satisfy the constraints of the - * RelativeLayout object. - *

- * The grid layout manager determines the size of individual - * components by dividing the free space in the container into - * equal-sized portions according to the number of rows and columns - * in the layout. The container's free space equals the container's - * size minus any insets and any specified horizontal or vertical - * gap. All components in a grid layout are given the same size. - * - * @param target the container in which to do the layout - * @see java.awt.Container - * @see java.awt.Container#doLayout - */ - public void layoutContainer(Container parent) - { - synchronized (parent.getTreeLock()) - { - if (axis == X_AXIS) - layoutContainerHorizontally(parent); - else - layoutContainerVertically(parent); - } - } - - /* - * Lay out all the components in the Container along the X-Axis - */ - private void layoutContainerHorizontally(Container parent) - { - int components = parent.getComponentCount(); - int visibleComponents = getVisibleComponents( parent ); - - if (components == 0) return; - - // Determine space available for components using relative sizing - - float relativeTotal = 0.0f; - Insets insets = parent.getInsets(); - int spaceAvailable = parent.getSize().width - - insets.left - - insets.right - - ((visibleComponents - 1) * gap) - - (2 * borderGap); - - for (int i = 0 ; i < components ; i++) - { - Component component = parent.getComponent(i); - - if (! component.isVisible()) continue; - - Float constraint = constraints.get(component); - - if (constraint == null) - { - Dimension d = component.getPreferredSize(); - spaceAvailable -= d.width; - } - else - { - relativeTotal += constraint.doubleValue(); - } - } - - // Allocate space to each component using relative sizing - - int[] relativeSpace = allocateRelativeSpace(parent, spaceAvailable, relativeTotal); - - // Position each component in the container - - int x = insets.left + borderGap; - int y = insets.top; - int insetGap = insets.top + insets.bottom; - int parentHeight = parent.getSize().height - insetGap; - - for (int i = 0 ; i < components ; i++) - { - Component component = parent.getComponent(i); - - if (! component.isVisible()) continue; - - if (i > 0) - x += gap; - - Dimension d = component.getPreferredSize(); - - if (fill) - d.height = parentHeight - fillGap; - - Float constraint = constraints.get(component); - - if (constraint == null) - { - component.setSize( d ); - int locationY = getLocationY(component, parentHeight) + y; - component.setLocation(x, locationY); - x += d.width; - } - else - { - int width = relativeSpace[i]; - component.setSize(width, d.height); - int locationY = getLocationY(component, parentHeight) + y; - component.setLocation(x, locationY); - x += width; - } - } - } - - /* - * Align the component on the Y-Axis - */ - private int getLocationY(Component component, int height) - { - // Use the Container alignment policy - - float alignmentY = alignment; - - // Override with the Component alignment - - if (alignmentY == COMPONENT) - alignmentY = component.getAlignmentY(); - - float y = (height - component.getSize().height) * alignmentY; - return (int)y; - } - - /* - * Lay out all the components in the Container along the Y-Axis - */ - private void layoutContainerVertically(Container parent) - { - int components = parent.getComponentCount(); - int visibleComponents = getVisibleComponents( parent ); - - if (components == 0) return; - - // Determine space available for components using relative sizing - - float relativeTotal = 0.0f; - Insets insets = parent.getInsets(); - int spaceAvailable = parent.getSize().height - - insets.top - - insets.bottom - - ((visibleComponents - 1) * gap) - - (2 * borderGap); - - for (int i = 0 ; i < components ; i++) - { - Component component = parent.getComponent(i); - - if (! component.isVisible()) continue; - - Float constraint = constraints.get(component); - - if (constraint == null) - { - Dimension d = component.getPreferredSize(); - spaceAvailable -= d.height; - } - else - { - relativeTotal += constraint.doubleValue(); - } - } - - // Allocate space to each component using relative sizing - - int[] relativeSpace = allocateRelativeSpace(parent, spaceAvailable, relativeTotal); - - // Position each component in the container - - int x = insets.left; - int y = insets.top + borderGap; - int insetGap = insets.left + insets.right; - int parentWidth = parent.getSize().width - insetGap; - - for (int i = 0 ; i < components ; i++) - { - Component component = parent.getComponent(i); - - if (! component.isVisible()) continue; - - if (i > 0) - y += gap; - - Dimension d = component.getPreferredSize(); - - if (fill) - d.width = parentWidth - fillGap; - - Float constraint = constraints.get(component); - - if (constraint == null) - { - component.setSize( d ); - int locationX = getLocationX(component, parentWidth) + x; - component.setLocation(locationX, y); - y += d.height; - } - else - { - int height = relativeSpace[i]; - component.setSize(d.width, height); - int locationX = getLocationX(component, parentWidth) + x; - component.setLocation(locationX, y); - y += height; - } - - } - } - - /* - * Align the component on the X-Axis - */ - private int getLocationX(Component component, int width) - { - // Use the Container alignment policy - - float alignmentX = alignment; - - // Override with the Component alignment - - if (alignmentX == COMPONENT) - alignmentX = component.getAlignmentX(); - - float x = (width - component.getSize().width) * alignmentX; - return (int)x; - } - - /* - * Allocate the space available to each component using relative sizing - */ - private int[] allocateRelativeSpace(Container parent, int spaceAvailable, float relativeTotal) - { - int spaceUsed = 0; - int components = parent.getComponentCount(); - int[] relativeSpace = new int[components]; - - for (int i = 0 ; i < components ; i++) - { - relativeSpace[i] = 0; - - if (relativeTotal > 0 && spaceAvailable > 0) - { - Component component = parent.getComponent(i); - Float constraint = constraints.get(component); - - if (constraint != null) - { - int space = (int)(spaceAvailable * constraint.floatValue() / relativeTotal); - relativeSpace[i] = space; - spaceUsed += space; - } - } - } - - int spaceRemaining = spaceAvailable - spaceUsed; - - if (relativeTotal > 0 && spaceRemaining > 0) - adjustForRounding(relativeSpace, spaceRemaining); - - return relativeSpace; - } - - /* - * Because of rounding, all the space has not been allocated - * Override this method to create a custom rounding policy - */ - protected void adjustForRounding(int[] relativeSpace, int spaceRemaining) - { - switch(roundingPolicy) - { - case DO_NOTHING: - break; - case FIRST: - adjustFirst(relativeSpace, spaceRemaining); - break; - case LAST: - adjustLast(relativeSpace, spaceRemaining); - break; - case LARGEST: - adjustLargest(relativeSpace, spaceRemaining); - break; - case EQUAL: - adjustEqual(relativeSpace, spaceRemaining); - break; - default: - adjustLargest(relativeSpace, spaceRemaining); - } - } - - /* - * First component using relative sizing gets all the space - */ - private void adjustFirst(int[] relativeSpace, int spaceRemaining) - { - for (int i = 0; i < relativeSpace.length; i++) - { - if (relativeSpace[i] > 0) - { - relativeSpace[i] += spaceRemaining; - break; - } - } - } - - /* - * Last component using relative sizing gets all the space - */ - private void adjustLast(int[] relativeSpace, int spaceRemaining) - { - for (int i = relativeSpace.length - 1; i > 0; i--) - { - if (relativeSpace[i] > 0) - { - relativeSpace[i] += spaceRemaining; - break; - } - } - } - - /* - * Largest component using relative sizing gets all the space. - * When multiple components are the same size, the last one found is used. - */ - private void adjustLargest(int[] relativeSpace, int spaceRemaining) - { - int largest = 0; - int largestSpace = 0; - - for (int i = 0; i < relativeSpace.length; i++) - { - int space = relativeSpace[i]; - - if (space > 0) - { - if (largestSpace < space) - { - largestSpace = space; - largest = i; - } - } - } - - relativeSpace[largest] += spaceRemaining; - } - - /* - * Each component using relative sizing gets 1 more pixel - * until all the space is used, starting with the first. - */ - private void adjustEqual(int[] relativeSpace, int spaceRemaining) - { - for (int i = 0; i < relativeSpace.length; i++) - { - if (relativeSpace[i] > 0) - { - relativeSpace[i]++; - spaceRemaining--; - - if (spaceRemaining == 0) - break; - } - } - } - - /* - * Determine the Preferred or Minimum layout size - */ - private Dimension getLayoutSize(Container parent, int type) - { - int width = 0; - int height = 0; - int components = parent.getComponentCount(); - int visibleComponents = getVisibleComponents( parent ); - - for (int i = 0 ; i < components ; i++) - { - Component component = parent.getComponent(i); - - if (! component.isVisible()) continue; - - Dimension d = getDimension(component, type); - - if (axis == X_AXIS) - { - width += d.width; - height = Math.max(height, d.height); - } - else - { - width = Math.max(width, d.width); - height += d.height; - } - } - - Insets insets = parent.getInsets(); - int totalGap = ((visibleComponents - 1) * gap) + (2 * borderGap); - - if (axis == X_AXIS) - { - width += insets.left + insets.right + totalGap; - height += insets.top + insets.bottom; - } - else - { - width += insets.left + insets.right; - height += insets.top + insets.bottom + totalGap; - } - - Dimension size = new Dimension(width, height); - return size; - } - - private int getVisibleComponents(Container container) - { - int visibleComponents = 0; - - for (Component component : container.getComponents()) - { - if (component.isVisible()) - visibleComponents++; - } - - return visibleComponents; - } - - private Dimension getDimension(Component component, int type) - { - switch (type) - { - case PREFERRED: return component.getPreferredSize(); - case MINIMUM: return component.getMinimumSize(); - default: return new Dimension(0, 0); - } - } - - /** - * There is no maximum. - */ - public Dimension maximumLayoutSize(Container target) - { - return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); - } - - /** - * Returns the alignment along the x axis. Use center alignment. - */ - public float getLayoutAlignmentX(Container parent) - { - return 0.5f; - } - - /** - * Returns the alignment along the y axis. Use center alignment. - */ - public float getLayoutAlignmentY(Container parent) - { - return 0.5f; - } - - /** - * Invalidates the layout, indicating that if the layout manager - * has cached information it should be discarded. - */ - public void invalidateLayout(Container target) - { - // remove constraints here? - } - - /** - * Returns the string representation of this column layout's values. - * @return a string representation of this grid layout - */ - public String toString() - { - return getClass().getName() - + "[axis=" + axis - + ",gap=" + gap - + "]"; - } + /** + * Creates a relative layout with the components layed out on the specified axis + * using the default gap + * + * @param axis X-AXIS or Y_AXIS + */ + public RelativeLayout(int axis) { + this(axis, 0); + } + + /** + * Creates a relative layout with the components layed out on the specified axis + * using the specfied gap + *

+ * All RelativeLayout constructors defer to this one. + * + * @param axis X-AXIS or Y_AXIS + * @param gap the gap + */ + public RelativeLayout(int axis, int gap) { + setAxis(axis); + setGap(gap); + setBorderGap(gap); + } + + /** + * Gets the layout axis. + * + * @return the layout axis + */ + public int getAxis() { + return axis; + } + + /** + * Sets the layout axis + * + * @param axis the layout axis + */ + public final void setAxis(int axis) { + if (axis != X_AXIS && axis != Y_AXIS) { + throw new IllegalArgumentException("invalid axis specified"); + } + + this.axis = axis; + } + + /** + * Gets the gap between components. + * + * @return the gap between components + */ + public int getGap() { + return gap; + } + + /** + * Sets the gap between components to the specified value. + * + * @param gap the gap between components + */ + public final void setGap(int gap) { + this.gap = gap < 0 ? 0 : gap; + } + + /** + * Gets the initial gap. This gap is used before the leading component + * and after the trailing component. + * + * @return the leading/trailing gap + */ + public int getBorderGap() { + return borderGap; + } + + /** + * Sets the initial gap. This gap is used before the leading component + * and after the trailing component. The default is set to the gap. + * + * @param borderGap the leading/trailing gap + */ + public final void setBorderGap(int borderGap) { + this.borderGap = borderGap < 0 ? 0 : borderGap; + } + + /** + * Gets the alignment of the components on the opposite axis. + * + * @return the alignment + */ + public float getAlignment() { + return alignment; + } + + /* + * Set the alignment of the component on the opposite axis. + * + * For X-AXIS this would refer to the Y alignemt. + * For Y-AXIS this would refer to the X alignment. + * + * Must be between 0.0 and 1.0, or -1. Values can be specified using: + * + * RelativeLayout.LEADING + * RelativeLayout.CENTER + * RelativeLayout.TRAILING + * RelativeLayout.COMPONENT - the getAlignemntX/Y method for the opposite axis will be used + */ + public void setAlignment(float alignment) { + this.alignment = alignment > 1.0f ? 1.0f : alignment < 0.0f ? -1.0f : alignment; + } + + /** + * Gets the fill property for the component size on the opposite edge. + * + * @return the fill property + */ + public boolean isFill() { + return fill; + } + + /** + * Change size of relative components to fill the space available For X-AXIS aligned components the height will be + * filled. For Y-AXIS aligned components the width will be filled. + */ + public void setFill(boolean fill) { + this.fill = fill; + } + + /** + * Gets the fill gap amount. + * + * @return the fill gap value + */ + public int getFillGap() { + return fillGap; + } + + /** + * Specify the number of pixels by which the fill size is decreased when setFill(true) has been specified. + */ + public void setFillGap(int fillGap) { + this.fillGap = fillGap; + } + + /** + * Gets the rounding policy. + * + * @return the rounding policy + */ + public int getRoundingPolicy() { + return roundingPolicy; + } + + /** + * Specify the rounding policy to be used when all the avialable pixels have not been allocated to a component. + * + * DO_NOTHING FIRST - extra pixels added to the first relative component LAST - extra pixels added to the last + * relative component LARGEST (default) - extra pixels added to the larger relative component EQUAL - a single pixel + * is added to each relative component (until pixels are used up) + */ + public void setRoundingPolicy(int roundingPolicy) { + this.roundingPolicy = roundingPolicy; + } + + /** + * Gets the constraints for the specified component. + * + * @param component the component to be queried + * @return the constraint for the specified component, or null if component is null or is not present in this layout + */ + public Float getConstraints(Component component) { + return constraints.get(component); + } + + /** + * Not supported + */ + @Override + public void addLayoutComponent(String name, Component component) { + } + + /* + * Keep track of any specified constraint for the component. + */ + @Override + public void addLayoutComponent(Component component, Object constraint) { + if (constraint != null) { + if (constraint instanceof Float) { + constraints.put(component, (Float) constraint); + } else { + throw new IllegalArgumentException("Constraint parameter must be of type Float"); + } + } + } + + /** + * Removes the specified component from the layout. + * + * @param comp the component to be removed + */ + @Override + public void removeLayoutComponent(Component comp) { + } + + /** + * Determines the preferred size of the container argument using this column layout.

The preferred width of a + * column layout is the largest preferred width of each column in the container, plus the horizontal padding times + * the number of columns minus one, plus the left and right insets of the target container.

The preferred height + * of a column layout is the largest preferred height of each row in the container, plus the vertical padding times + * the number of rows minus one, plus the top and bottom insets of the target container. + * + * @param target the container in which to do the layout + * @return the preferred dimensions to lay out the subcomponents of the specified container + * @see java.awt.RelativeLayout#minimumLayoutSize + * @see java.awt.Container#getPreferredSize() + */ + @Override + public Dimension preferredLayoutSize(Container parent) { + synchronized (parent.getTreeLock()) { + return getLayoutSize(parent, PREFERRED); + } + } + + /** + * Determines the minimum size of the container argument using this column layout.

The minimum width of a grid + * layout is the largest minimum width of each column in the container, plus the horizontal padding times the number + * of columns minus one, plus the left and right insets of the target container.

The minimum height of a column + * layout is the largest minimum height of each row in the container, plus the vertical padding times the number of + * rows minus one, plus the top and bottom insets of the target container. + * + * @param target the container in which to do the layout + * @return the minimum dimensions needed to lay out the subcomponents of the specified container + * @see java.awt.RelativeLayout#preferredLayoutSize + * @see java.awt.Container#doLayout + */ + @Override + public Dimension minimumLayoutSize(Container parent) { + synchronized (parent.getTreeLock()) { + return getLayoutSize(parent, MINIMUM); + } + } + + /** + * Lays out the specified container using this layout.

This method reshapes the components in the specified + * target container in order to satisfy the constraints of the + * RelativeLayout object.

The grid layout manager determines the size of individual components by + * dividing the free space in the container into equal-sized portions according to the number of rows and columns in + * the layout. The container's free space equals the container's size minus any insets and any specified horizontal + * or vertical gap. All components in a grid layout are given the same size. + * + * @param target the container in which to do the layout + * @see java.awt.Container + * @see java.awt.Container#doLayout + */ + @Override + public void layoutContainer(Container parent) { + synchronized (parent.getTreeLock()) { + if (axis == X_AXIS) { + layoutContainerHorizontally(parent); + } else { + layoutContainerVertically(parent); + } + } + } + + /* + * Lay out all the components in the Container along the X-Axis + */ + private void layoutContainerHorizontally(Container parent) { + int components = parent.getComponentCount(); + int visibleComponents = getVisibleComponents(parent); + + if (components == 0) { + return; + } + + // Determine space available for components using relative sizing + + float relativeTotal = 0.0f; + Insets insets = parent.getInsets(); + int spaceAvailable = parent.getSize().width + - insets.left + - insets.right + - ((visibleComponents - 1) * gap) + - (2 * borderGap); + + for (int i = 0; i < components; i++) { + Component component = parent.getComponent(i); + + if (!component.isVisible()) { + continue; + } + + Float constraint = constraints.get(component); + + if (constraint == null) { + Dimension d = component.getPreferredSize(); + spaceAvailable -= d.width; + } else { + relativeTotal += constraint.doubleValue(); + } + } + + // Allocate space to each component using relative sizing + + int[] relativeSpace = allocateRelativeSpace(parent, spaceAvailable, relativeTotal); + + // Position each component in the container + + int x = insets.left + borderGap; + int y = insets.top; + int insetGap = insets.top + insets.bottom; + int parentHeight = parent.getSize().height - insetGap; + + for (int i = 0; i < components; i++) { + Component component = parent.getComponent(i); + + if (!component.isVisible()) { + continue; + } + + if (i > 0) { + x += gap; + } + + Dimension d = component.getPreferredSize(); + + if (fill) { + d.height = parentHeight - fillGap; + } + + Float constraint = constraints.get(component); + + if (constraint == null) { + component.setSize(d); + int locationY = getLocationY(component, parentHeight) + y; + component.setLocation(x, locationY); + x += d.width; + } else { + int width = relativeSpace[i]; + component.setSize(width, d.height); + int locationY = getLocationY(component, parentHeight) + y; + component.setLocation(x, locationY); + x += width; + } + } + } + + /* + * Align the component on the Y-Axis + */ + private int getLocationY(Component component, int height) { + // Use the Container alignment policy + + float alignmentY = alignment; + + // Override with the Component alignment + + if (alignmentY == COMPONENT) { + alignmentY = component.getAlignmentY(); + } + + return (int) ((height - component.getSize().height) * alignmentY); + } + + /* + * Lay out all the components in the Container along the Y-Axis + */ + private void layoutContainerVertically(Container parent) { + int components = parent.getComponentCount(); + int visibleComponents = getVisibleComponents(parent); + + if (components == 0) { + return; + } + + // Determine space available for components using relative sizing + + float relativeTotal = 0.0f; + Insets insets = parent.getInsets(); + int spaceAvailable = parent.getSize().height + - insets.top + - insets.bottom + - ((visibleComponents - 1) * gap) + - (2 * borderGap); + + for (int i = 0; i < components; i++) { + Component component = parent.getComponent(i); + + if (!component.isVisible()) { + continue; + } + + Float constraint = constraints.get(component); + + if (constraint == null) { + Dimension d = component.getPreferredSize(); + spaceAvailable -= d.height; + } else { + relativeTotal += constraint.doubleValue(); + } + } + + // Allocate space to each component using relative sizing + + int[] relativeSpace = allocateRelativeSpace(parent, spaceAvailable, relativeTotal); + + // Position each component in the container + + int x = insets.left; + int y = insets.top + borderGap; + int insetGap = insets.left + insets.right; + int parentWidth = parent.getSize().width - insetGap; + + for (int i = 0; i < components; i++) { + Component component = parent.getComponent(i); + + if (!component.isVisible()) { + continue; + } + + if (i > 0) { + y += gap; + } + + Dimension d = component.getPreferredSize(); + + if (fill) { + d.width = parentWidth - fillGap; + } + + Float constraint = constraints.get(component); + + if (constraint == null) { + component.setSize(d); + int locationX = getLocationX(component, parentWidth) + x; + component.setLocation(locationX, y); + y += d.height; + } else { + int height = relativeSpace[i]; + component.setSize(d.width, height); + int locationX = getLocationX(component, parentWidth) + x; + component.setLocation(locationX, y); + y += height; + } + + } + } + + /* + * Align the component on the X-Axis + */ + private int getLocationX(Component component, int width) { + // Use the Container alignment policy + + float alignmentX = alignment; + + // Override with the Component alignment + + if (alignmentX == COMPONENT) { + alignmentX = component.getAlignmentX(); + } + + return (int) ((width - component.getSize().width) * alignmentX); + } + + /* + * Allocate the space available to each component using relative sizing + */ + private int[] allocateRelativeSpace(Container parent, int spaceAvailable, float relativeTotal) { + int spaceUsed = 0; + int components = parent.getComponentCount(); + int[] relativeSpace = new int[components]; + + for (int i = 0; i < components; i++) { + relativeSpace[i] = 0; + + if (relativeTotal > 0 && spaceAvailable > 0) { + Component component = parent.getComponent(i); + Float constraint = constraints.get(component); + + if (constraint != null) { + int space = (int) (spaceAvailable * constraint.floatValue() / relativeTotal); + relativeSpace[i] = space; + spaceUsed += space; + } + } + } + + int spaceRemaining = spaceAvailable - spaceUsed; + + if (relativeTotal > 0 && spaceRemaining > 0) { + adjustForRounding(relativeSpace, spaceRemaining); + } + + return relativeSpace; + } + + /* + * Because of rounding, all the space has not been allocated + * Override this method to create a custom rounding policy + */ + protected void adjustForRounding(int[] relativeSpace, int spaceRemaining) { + switch (roundingPolicy) { + case DO_NOTHING: + break; + case FIRST: + adjustFirst(relativeSpace, spaceRemaining); + break; + case LAST: + adjustLast(relativeSpace, spaceRemaining); + break; + case LARGEST: + adjustLargest(relativeSpace, spaceRemaining); + break; + case EQUAL: + adjustEqual(relativeSpace, spaceRemaining); + break; + default: + adjustLargest(relativeSpace, spaceRemaining); + } + } + + /* + * First component using relative sizing gets all the space + */ + private void adjustFirst(int[] relativeSpace, int spaceRemaining) { + for (int i = 0; i < relativeSpace.length; i++) { + if (relativeSpace[i] > 0) { + relativeSpace[i] += spaceRemaining; + break; + } + } + } + + /* + * Last component using relative sizing gets all the space + */ + private void adjustLast(int[] relativeSpace, int spaceRemaining) { + for (int i = relativeSpace.length - 1; i > 0; i--) { + if (relativeSpace[i] > 0) { + relativeSpace[i] += spaceRemaining; + break; + } + } + } + + /* + * Largest component using relative sizing gets all the space. + * When multiple components are the same size, the last one found is used. + */ + private void adjustLargest(int[] relativeSpace, int spaceRemaining) { + int largest = 0; + int largestSpace = 0; + + for (int i = 0; i < relativeSpace.length; i++) { + int space = relativeSpace[i]; + + if (space > 0) { + if (largestSpace < space) { + largestSpace = space; + largest = i; + } + } + } + + relativeSpace[largest] += spaceRemaining; + } + + /* + * Each component using relative sizing gets 1 more pixel + * until all the space is used, starting with the first. + */ + private void adjustEqual(int[] relativeSpace, int spaceRemaining) { + int pixelsLeft = spaceRemaining; + for (int i = 0; i < relativeSpace.length; i++) { + if (relativeSpace[i] > 0) { + relativeSpace[i]++; + pixelsLeft--; + + if (pixelsLeft == 0) { + break; + } + } + } + } + + /* + * Determine the Preferred or Minimum layout size + */ + private Dimension getLayoutSize(Container parent, int type) { + int width = 0; + int height = 0; + int components = parent.getComponentCount(); + int visibleComponents = getVisibleComponents(parent); + + for (int i = 0; i < components; i++) { + Component component = parent.getComponent(i); + + if (!component.isVisible()) { + continue; + } + + Dimension d = getDimension(component, type); + + if (axis == X_AXIS) { + width += d.width; + height = Math.max(height, d.height); + } else { + width = Math.max(width, d.width); + height += d.height; + } + } + + Insets insets = parent.getInsets(); + int totalGap = ((visibleComponents - 1) * gap) + (2 * borderGap); + + if (axis == X_AXIS) { + width += insets.left + insets.right + totalGap; + height += insets.top + insets.bottom; + } else { + width += insets.left + insets.right; + height += insets.top + insets.bottom + totalGap; + } + + return new Dimension(width, height); + } + + private int getVisibleComponents(Container container) { + int visibleComponents = 0; + + for (Component component : container.getComponents()) { + if (component.isVisible()) { + visibleComponents++; + } + } + + return visibleComponents; + } + + private Dimension getDimension(Component component, int type) { + switch (type) { + case PREFERRED: + return component.getPreferredSize(); + case MINIMUM: + return component.getMinimumSize(); + default: + return new Dimension(0, 0); + } + } + + /** + * There is no maximum. + */ + @Override + public Dimension maximumLayoutSize(Container target) { + return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); + } + + /** + * Returns the alignment along the x axis. Use center alignment. + */ + @Override + public float getLayoutAlignmentX(Container parent) { + return 0.5f; + } + + /** + * Returns the alignment along the y axis. Use center alignment. + */ + @Override + public float getLayoutAlignmentY(Container parent) { + return 0.5f; + } + + /** + * Invalidates the layout, indicating that if the layout manager has cached information it should be discarded. + */ + @Override + public void invalidateLayout(Container target) { + // remove constraints here? + } + + /** + * Returns the string representation of this column layout's values. + * + * @return a string representation of this grid layout + */ + @Override + public String toString() { + return getClass().getName() + + "[axis=" + axis + + ",gap=" + gap + + "]"; + } } diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java index 72f0f83d5f..7cf978025b 100644 --- a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java +++ b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java @@ -71,6 +71,8 @@ import java.util.UUID; */ public class MageBook extends JComponent { + private static final long serialVersionUID = 1L; + public static final String LAYOUT_3x3 = "small"; public static final String LAYOUT_4x4 = "big"; @@ -181,7 +183,7 @@ public class MageBook extends JComponent { public void execute() { if (!currentSet.equals(_set) || currentPage != 0) { AudioManager.playAnotherTab(); - synchronized (this) { + synchronized (MageBook.this) { selectedTab = _index; } currentPage = 0; @@ -413,17 +415,17 @@ public class MageBook extends JComponent { private java.util.List tabs = new ArrayList(); private int selectedTab; - static private final String CENTER_PANEL_IMAGE_PATH = "/book_bg.jpg"; - static private final String RIGHT_PANEL_IMAGE_PATH = "/book_right.jpg"; - static private final String LEFT_PANEL_IMAGE_PATH = "/book_left.jpg"; - static private final String LEFT_PAGE_BUTTON_IMAGE_PATH = "/book_pager_left.png"; - static private final String RIGHT_PAGE_BUTTON_IMAGE_PATH = "/book_pager_right.png"; - static private final String LEFT_TAB_IMAGE_PATH = "/tab_left.png"; - static private final String RIGHT_TAB_IMAGE_PATH = "/tab_right.png"; - static private final int OFFSET_X = 25; - static private final int OFFSET_Y = 20; - static private final int LEFT_RIGHT_PAGES_WIDTH = 40; - static private final Color NOT_IMPLEMENTED = new Color(220, 220, 220, 150); + private static final String CENTER_PANEL_IMAGE_PATH = "/book_bg.jpg"; + private static final String RIGHT_PANEL_IMAGE_PATH = "/book_right.jpg"; + private static final String LEFT_PANEL_IMAGE_PATH = "/book_left.jpg"; + private static final String LEFT_PAGE_BUTTON_IMAGE_PATH = "/book_pager_left.png"; + private static final String RIGHT_PAGE_BUTTON_IMAGE_PATH = "/book_pager_right.png"; + private static final String LEFT_TAB_IMAGE_PATH = "/tab_left.png"; + private static final String RIGHT_TAB_IMAGE_PATH = "/tab_right.png"; + private static final int OFFSET_X = 25; + private static final int OFFSET_Y = 20; + private static final int LEFT_RIGHT_PAGES_WIDTH = 40; + private static final Color NOT_IMPLEMENTED = new Color(220, 220, 220, 150); private Configuration conf; } diff --git a/Mage.Client/src/main/java/mage/client/table/TablesPane.java b/Mage.Client/src/main/java/mage/client/table/TablesPane.java index adc0e68224..9d0165c517 100644 --- a/Mage.Client/src/main/java/mage/client/table/TablesPane.java +++ b/Mage.Client/src/main/java/mage/client/table/TablesPane.java @@ -25,16 +25,8 @@ * authors and should not be interpreted as representing official policies, either expressed * or implied, of BetaSteward_at_googlemail.com. */ - -/* - * TablesPane.java - * - * Created on Dec 17, 2009, 9:21:42 AM - */ - package mage.client.table; -import java.util.Map; import java.util.UUID; import javax.swing.JComponent; @@ -54,8 +46,7 @@ public class TablesPane extends MagePane { boolean initialized = false; if (Plugins.getInstance().isThemePluginLoaded()) { tablesPanel = new mage.client.table.TablesPanel(); - Map ui = tablesPanel.getUIComponents(); - JComponent container = Plugins.getInstance().updateTablePanel(ui); + JComponent container = Plugins.getInstance().updateTablePanel(tablesPanel.getUIComponents()); if (container != null) { initComponents(container); container.add(tablesPanel); diff --git a/Mage.Client/src/main/java/mage/client/table/TablesPanel.java b/Mage.Client/src/main/java/mage/client/table/TablesPanel.java index 4e6a05bd5c..55ffe7c044 100644 --- a/Mage.Client/src/main/java/mage/client/table/TablesPanel.java +++ b/Mage.Client/src/main/java/mage/client/table/TablesPanel.java @@ -73,7 +73,7 @@ import java.util.concurrent.ExecutionException; */ public class TablesPanel extends javax.swing.JPanel { - private final static Logger logger = Logger.getLogger(TablesPanel.class); + private static final Logger logger = Logger.getLogger(TablesPanel.class); private TableTableModel tableModel; private MatchesTableModel matchesModel; @@ -674,7 +674,7 @@ class UpdateTablesTask extends SwingWorker> { private UUID roomId; private TablesPanel panel; - private final static Logger logger = Logger.getLogger(UpdateTablesTask.class); + private static final Logger logger = Logger.getLogger(UpdateTablesTask.class); private int count = 0; @@ -723,7 +723,7 @@ class UpdatePlayersTask extends SwingWorker> { private UUID roomId; private ChatPanel chat; - private final static Logger logger = Logger.getLogger(UpdatePlayersTask.class); + private static final Logger logger = Logger.getLogger(UpdatePlayersTask.class); UpdatePlayersTask(Session session, UUID roomId, ChatPanel chat) { this.session = session; @@ -829,7 +829,7 @@ class UpdateMatchesTask extends SwingWorker> { private UUID roomId; private TablesPanel panel; - private final static Logger logger = Logger.getLogger(UpdateTablesTask.class); + private static final Logger logger = Logger.getLogger(UpdateTablesTask.class); UpdateMatchesTask(Session session, UUID roomId, TablesPanel panel) { this.session = session; diff --git a/Mage.Client/src/main/java/org/mage/card/arcane/Animation.java b/Mage.Client/src/main/java/org/mage/card/arcane/Animation.java index f981b3a550..4d4fe569ed 100644 --- a/Mage.Client/src/main/java/org/mage/card/arcane/Animation.java +++ b/Mage.Client/src/main/java/org/mage/card/arcane/Animation.java @@ -8,16 +8,13 @@ import java.util.Timer; import java.util.TimerTask; abstract public class Animation { - static private final long TARGET_MILLIS_PER_FRAME = 30; - //static private final float HALF_PI = (float)(Math.PI / 2); + private static final long TARGET_MILLIS_PER_FRAME = 30; - static private Timer timer = new Timer("Animation", true); + private static Timer timer = new Timer("Animation", true); - //static private CardPanel delayedCardPanel; - //static private long delayedTime; - static private CardPanel enlargedCardPanel; - static private CardPanel enlargedAnimationPanel; - static private Object enlargeLock = new Object(); + private static CardPanel enlargedCardPanel; + private static CardPanel enlargedAnimationPanel; + private static final Object enlargeLock = new Object(); private TimerTask timerTask; private FrameTimer frameTimer; @@ -29,6 +26,7 @@ abstract public class Animation { public Animation (final long duration, long delay) { timerTask = new TimerTask() { + @Override public void run () { if (frameTimer == null) { start(); @@ -40,7 +38,9 @@ abstract public class Animation { elapsed = duration; } update(elapsed / (float)duration); - if (elapsed == duration) end(); + if (elapsed == duration) { + end(); + } } }; timer.scheduleAtFixedRate(timerTask, delay, TARGET_MILLIS_PER_FRAME); @@ -62,32 +62,36 @@ abstract public class Animation { /** * Uses averaging of the time between the past few frames to provide smooth animation. */ - private class FrameTimer { - static private final int SAMPLES = 6; - static private final long MAX_FRAME = 100; // Max time for one frame, to weed out spikes. + private static class FrameTimer { + private static final int SAMPLES = 6; + private static final long MAX_FRAME = 100; // Max time for one frame, to weed out spikes. private long samples[] = new long[SAMPLES]; private int sampleIndex; public FrameTimer () { long currentTime = System.currentTimeMillis(); - for (int i = SAMPLES - 1; i >= 0; i--) + for (int i = SAMPLES - 1; i >= 0; i--) { samples[i] = currentTime - (SAMPLES - i) * TARGET_MILLIS_PER_FRAME; + } } public long getTimeSinceLastFrame () { long currentTime = System.currentTimeMillis(); int id = sampleIndex - 1; - if (id < 0) id += SAMPLES; + if (id < 0) { + id += SAMPLES; + } long timeSinceLastSample = currentTime - samples[id]; // If the slice was too big, advance all the previous times by the diff. if (timeSinceLastSample > MAX_FRAME) { long diff = timeSinceLastSample - MAX_FRAME; - for (int i = 0; i < SAMPLES; i++) + for (int i = 0; i < SAMPLES; i++) { samples[i] += diff; + } } long timeSinceOldestSample = currentTime - samples[sampleIndex]; @@ -100,26 +104,37 @@ abstract public class Animation { static public void tapCardToggle (final CardPanel panel, final MagePermanent parent, final boolean tapped, final boolean flipped) { new Animation(300) { + @Override protected void start () { parent.onBeginAnimation(); } + @Override protected void update (float percentage) { if (tapped) { panel.tappedAngle = CardPanel.TAPPED_ANGLE * percentage; // reverse movement if untapping - if (!panel.isTapped()) panel.tappedAngle = CardPanel.TAPPED_ANGLE - panel.tappedAngle; + if (!panel.isTapped()) { + panel.tappedAngle = CardPanel.TAPPED_ANGLE - panel.tappedAngle; + } } if (flipped) { - panel.flippedAngle = CardPanel.FLIPPED_ANGLE * percentage; - if (!panel.isFlipped()) panel.flippedAngle = CardPanel.FLIPPED_ANGLE - panel.flippedAngle; + panel.flippedAngle = CardPanel.FLIPPED_ANGLE * percentage; + if (!panel.isFlipped()) { + panel.flippedAngle = CardPanel.FLIPPED_ANGLE - panel.flippedAngle; + } } panel.repaint(); } + @Override protected void end () { - if (tapped) panel.tappedAngle = panel.isTapped() ? CardPanel.TAPPED_ANGLE : 0; - if (flipped) panel.flippedAngle = panel.isFlipped() ? CardPanel.FLIPPED_ANGLE : 0; + if (tapped) { + panel.tappedAngle = panel.isTapped() ? CardPanel.TAPPED_ANGLE : 0; + } + if (flipped) { + panel.flippedAngle = panel.isFlipped() ? CardPanel.FLIPPED_ANGLE : 0; + } parent.onEndAnimation(); parent.repaint(); } @@ -129,12 +144,14 @@ abstract public class Animation { public static void transformCard (final CardPanel panel, final MagePermanent parent, final boolean transformed) { new Animation(1200) { - boolean state = false; + private boolean state = false; + @Override protected void start () { parent.onBeginAnimation(); } + @Override protected void update (float percentage) { double p = percentage * 2; if (percentage > 0.5) { @@ -152,6 +169,7 @@ abstract public class Animation { panel.repaint(); } + @Override protected void end () { parent.onEndAnimation(); parent.repaint(); @@ -159,11 +177,11 @@ abstract public class Animation { }; } - // static public void moveCardToPlay (Component source, final CardPanel dest, final CardPanel animationPanel) { static public void moveCardToPlay (final int startX, final int startY, final int startWidth, final int endX, final int endY, final int endWidth, final CardPanel animationPanel, final CardPanel placeholder, final JLayeredPane layeredPane, final int speed) { UI.invokeLater(new Runnable() { + @Override public void run () { final int startHeight = Math.round(startWidth * CardPanel.ASPECT_RATIO); final int endHeight = Math.round(endWidth * CardPanel.ASPECT_RATIO); @@ -173,42 +191,46 @@ abstract public class Animation { animationPanel.setCardBounds(startX, startY, startWidth, startHeight); animationPanel.setAnimationPanel(true); Container parent = animationPanel.getParent(); - if (parent != layeredPane) { + if (parent != null && !parent.equals(layeredPane)) { layeredPane.add(animationPanel); layeredPane.setLayer(animationPanel, JLayeredPane.MODAL_LAYER); } new Animation(700) { + @Override protected void update (float percentage) { + float percent = percentage; if (placeholder != null && !placeholder.isShowing()) { cancel(); return; } - int currentX = startX + Math.round((endX - startX + endWidth / 2f) * percentage); - int currentY = startY + Math.round((endY - startY + endHeight / 2f) * percentage); + int currentX = startX + Math.round((endX - startX + endWidth / 2f) * percent); + int currentY = startY + Math.round((endY - startY + endHeight / 2f) * percent); int currentWidth, currentHeight; int midWidth = Math.max(200, endWidth * 2); int midHeight = Math.round(midWidth * CardPanel.ASPECT_RATIO); - if (percentage <= 0.5f) { - percentage = percentage * 2; - float pp = sqrta * (1 - percentage); - percentage = 1 - a * pp * pp; - currentWidth = startWidth + Math.round((midWidth - startWidth) * percentage); - currentHeight = startHeight + Math.round((midHeight - startHeight) * percentage); + if (percent <= 0.5f) { + percent = percent * 2; + float pp = sqrta * (1 - percent); + percent = 1 - a * pp * pp; + currentWidth = startWidth + Math.round((midWidth - startWidth) * percent); + currentHeight = startHeight + Math.round((midHeight - startHeight) * percent); } else { - percentage = (percentage - 0.5f) * 2; - float pp = sqrta * percentage; - percentage = a * pp * pp; - currentWidth = midWidth + Math.round((endWidth - midWidth) * percentage); - currentHeight = midHeight + Math.round((endHeight - midHeight) * percentage); + percent = (percent - 0.5f) * 2; + float pp = sqrta * percent; + percent = a * pp * pp; + currentWidth = midWidth + Math.round((endWidth - midWidth) * percent); + currentHeight = midHeight + Math.round((endHeight - midHeight) * percent); } - currentX -= Math.round(currentWidth / 2); - currentY -= Math.round(currentHeight / 2); + currentX -= currentWidth / 2; + currentY -= currentHeight / 2; animationPanel.setCardBounds(currentX, currentY, currentWidth, currentHeight); } + @Override protected void end () { EventQueue.invokeLater(new Runnable() { + @Override public void run () { if (placeholder != null) { placeholder.setDisplayEnabled(true); @@ -229,6 +251,7 @@ abstract public class Animation { final int endWidth, final CardPanel animationPanel, final CardPanel placeholder, final JLayeredPane layeredPane, final int speed) { UI.invokeLater(new Runnable() { + @Override public void run () { final int startHeight = Math.round(startWidth * CardPanel.ASPECT_RATIO); final int endHeight = Math.round(endWidth * CardPanel.ASPECT_RATIO); @@ -236,12 +259,13 @@ abstract public class Animation { animationPanel.setCardBounds(startX, startY, startWidth, startHeight); animationPanel.setAnimationPanel(true); Container parent = animationPanel.getParent(); - if (parent != layeredPane) { + if (parent != null && !parent.equals(layeredPane)) { layeredPane.add(animationPanel); layeredPane.setLayer(animationPanel, JLayeredPane.MODAL_LAYER); } new Animation(speed) { + @Override protected void update (float percentage) { int currentX = startX + Math.round((endX - startX) * percentage); int currentY = startY + Math.round((endY - startY) * percentage); @@ -250,8 +274,10 @@ abstract public class Animation { animationPanel.setCardBounds(currentX, currentY, currentWidth, currentHeight); } + @Override protected void end () { EventQueue.invokeLater(new Runnable() { + @Override public void run () { if (placeholder != null) { placeholder.setDisplayEnabled(true); @@ -269,18 +295,19 @@ abstract public class Animation { } static public void shrinkCard () { - CardPanel enlargedCardPanel, enlargedAnimationPanel; + final CardPanel overPanel, animationPanel; synchronized (enlargeLock) { //delayedCardPanel = null; //delayedTime = 0; - enlargedCardPanel = Animation.enlargedCardPanel; - enlargedAnimationPanel = Animation.enlargedAnimationPanel; - if (enlargedAnimationPanel == null) return; + overPanel = Animation.enlargedCardPanel; + animationPanel = Animation.enlargedAnimationPanel; + if (animationPanel == null) { + return; + } Animation.enlargedCardPanel = null; Animation.enlargedAnimationPanel = null; } - final CardPanel overPanel = enlargedCardPanel, animationPanel = enlargedAnimationPanel; animationPanel.setAnimationPanel(true); final JLayeredPane layeredPane = SwingUtilities.getRootPane(overPanel).getLayeredPane(); @@ -292,6 +319,7 @@ abstract public class Animation { final int endHeight = Math.round(endWidth * CardPanel.ASPECT_RATIO); new Animation(200) { + @Override protected void update (float percentage) { int currentWidth = startWidth + Math.round((endWidth - startWidth) * percentage); int currentHeight = startHeight + Math.round((endHeight - startHeight) * percentage); @@ -306,10 +334,12 @@ abstract public class Animation { animationPanel.setCardBounds(currentX, currentY, currentWidth, currentHeight); } + @Override protected void end () { animationPanel.setVisible(false); animationPanel.repaint(); EventQueue.invokeLater(new Runnable() { + @Override public void run () { layeredPane.remove(animationPanel); } @@ -329,15 +359,18 @@ abstract public class Animation { count = 1; } new Animation(600 / count) { + @Override protected void start () { } + @Override protected void update (float percentage) { float alpha = percentage; card.setAlpha(alpha); card.repaint(); } + @Override protected void end () { card.setAlpha(1.f); } @@ -349,15 +382,18 @@ abstract public class Animation { count = 1; } new Animation(600 / count) { + @Override protected void start () { } + @Override protected void update (float percentage) { float alpha = 1 - percentage; card.setAlpha(alpha); card.repaint(); } + @Override protected void end () { card.setAlpha(0f); } diff --git a/Mage.Client/src/main/java/org/mage/card/arcane/CardPanel.java b/Mage.Client/src/main/java/org/mage/card/arcane/CardPanel.java index dc972f054c..2d910b138b 100644 --- a/Mage.Client/src/main/java/org/mage/card/arcane/CardPanel.java +++ b/Mage.Client/src/main/java/org/mage/card/arcane/CardPanel.java @@ -37,12 +37,12 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti private static final Logger log = Logger.getLogger(CardPanel.class); - static public final double TAPPED_ANGLE = Math.PI / 2; - static public final double FLIPPED_ANGLE = Math.PI; - static public final float ASPECT_RATIO = 3.5f / 2.5f; - static public final int POPUP_X_GAP = 1; // prevent popup window from blinking + public static final double TAPPED_ANGLE = Math.PI / 2; + public static final double FLIPPED_ANGLE = Math.PI; + public static final float ASPECT_RATIO = 3.5f / 2.5f; + public static final int POPUP_X_GAP = 1; // prevent popup window from blinking - static public CardPanel dragAnimationPanel; + public static CardPanel dragAnimationPanel; public static final Rectangle CARD_SIZE_FULL = new Rectangle(101, 149); @@ -62,7 +62,7 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti public double tappedAngle = 0; public double flippedAngle = 0; - public ScaledImagePanel imagePanel; + public final ScaledImagePanel imagePanel; public ImagePanel overlayPanel; public JPanel buttonPanel; @@ -178,7 +178,6 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti add(imagePanel); imagePanel.setScaleLarger(true); imagePanel.setScalingType(ScalingType.nearestNeighbor); - imagePanel.setScalingBlur(true); imagePanel.setScalingMultiPassType(MultipassType.none); String cardType = getType(newGameCard); @@ -190,7 +189,9 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti try { tappedAngle = isTapped() ? CardPanel.TAPPED_ANGLE : 0; flippedAngle = isFlipped() ? CardPanel.FLIPPED_ANGLE : 0; - if (!loadImage || gameCard.isFaceDown()) return; + if (!loadImage || gameCard.isFaceDown()) { + return; + } BufferedImage srcImage = ImageCache.getThumbnail(gameCard); if (srcImage != null) { hasImage = true; @@ -255,51 +256,9 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti return zone; } + @Override public void setFoil(boolean foil) { this.isFoil = foil; - if (foil) { - /*BufferedImage source = BufferedImageBuilder.bufferImage(imagePanel.getSrcImage()); - HueFilter filter = FilterFactory.getHueFilter(); - filter.setHue(0.1f); - BufferedImage dest = filter.filter(source, null); - imagePanel.setImage(dest); - imagePanel.repaint();*/ - - /* - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - if (imagePanel.getSrcImage() == null) { - return; - } - BufferedImage source = BufferedImageBuilder.bufferImage(imagePanel.getSrcImage()); - HueFilter filter = FilterFactory.getHueFilter(); - float hue = 0.005f; - while (true) { - try { - Thread.sleep(DEFAULT_DELAY_PERIOD); - } catch (InterruptedException e) { - } - hue += 0.015F; - if (hue >= 1.0D) { - hue = 0.005F; - } - filter.setHue(hue); - final BufferedImage dest = filter.filter(source, null); - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - imagePanel.setImage(dest); - imagePanel.repaint(); - } - }); - } - } - }); - thread.setDaemon(false); - thread.start(); - */ - } } public void setScalingType(ScalingType scalingType) { @@ -341,8 +300,12 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti @Override public void paint(Graphics g) { - if (!displayEnabled) return; - if (!isValid()) super.validate(); + if (!displayEnabled) { + return; + } + if (!isValid()) { + super.validate(); + } Graphics2D g2d = (Graphics2D) g; if (transformAngle < 1) { float edgeOffset = (cardWidth + cardXOffset) / 2f; @@ -466,10 +429,11 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti ptText.setLocation(cardXOffset + ptX - TEXT_GLOW_SIZE / 2 - offsetX, cardYOffset + ptY - TEXT_GLOW_SIZE / 2); dayNightButton.setLocation(0, cardHeight - 30); - if (isAnimationPanel || cardWidth < 200) + if (isAnimationPanel || cardWidth < 200) { imagePanel.setScalingType(ScalingType.nearestNeighbor); - else + } else { imagePanel.setScalingType(ScalingType.bilinear); + } } @Override @@ -494,15 +458,6 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti setBounds(x + xOffset, y + yOffset, width, height); } - /*@Override - public void repaint() { - Rectangle b = getBounds(); - JRootPane rootPane = SwingUtilities.getRootPane(this); - if (rootPane == null) return; - Point p = SwingUtilities.convertPoint(getParent(), b.x, b.y, rootPane); - rootPane.repaint(p.x, p.y, b.width, b.height); - }*/ - public int getCardX() { return getX() + cardXOffset; } @@ -563,7 +518,9 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti try { tappedAngle = isTapped() ? CardPanel.TAPPED_ANGLE : 0; flippedAngle = isFlipped() ? CardPanel.FLIPPED_ANGLE : 0; - if (gameCard.isFaceDown()) return; + if (gameCard.isFaceDown()) { + return; + } BufferedImage srcImage = ImageCache.getThumbnail(gameCard); if (srcImage != null) { hasImage = true; @@ -666,41 +623,28 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti @Override public boolean contains(int x, int y) { - if (containsThis(x, y, true)) return true; - - /* - * if (attachedCount > 0) { for (MWCardImpl card : - * mwAttachedCards.keySet()) { if (card.contains(x, y)) return true; } } - */ + if (containsThis(x, y, true)) { + return true; + } return false; } public boolean containsThis(int x, int y, boolean root) { - //log.info("x="+x+", y="+y); Point component = getLocation(); - //int dy = component.y; - //if (root) dy = 0; - int cx = getCardX() - component.x; int cy = getCardY() - component.y; int cw = getCardWidth(); int ch = getCardHeight(); if (isTapped()) { - cy = ch - cw + cx /*+ attachedDy*attachedCount*/; + cy = ch - cw + cx; ch = cw; cw = getCardHeight(); } - //int dx = drawIcons ? 19 : 0; - //int dx = 0; if (x >= cx && x <= cx + cw && y >= cy && y <= cy + ch) { - //log.info("!cx="+cx+", cy="+cy+", dx="+cw +", ch="+ch); - //log.info(getOriginal().getId()); return true; - } else { - //log.info("cx="+cx+", cy="+cy+", dx="+cw +", ch="+ch); } return false; } @@ -712,8 +656,9 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti @Override public Image getImage() { - if (this.hasImage) + if (this.hasImage) { return ImageCache.getImageOriginal(gameCard); + } return null; } @@ -723,7 +668,9 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti @Override public void mouseEntered(MouseEvent e) { - if (gameCard.isFaceDown()) return; + if (gameCard.isFaceDown()) { + return; + } if (!popupShowing) { synchronized (this) { if (!popupShowing) { @@ -740,15 +687,21 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti @Override public void mouseMoved(MouseEvent e) { - if (gameCard.isFaceDown()) return; + if (gameCard.isFaceDown()) { + return; + } data.component = this; callback.mouseMoved(e, data); } @Override public void mouseExited(MouseEvent e) { - if (gameCard.isFaceDown()) return; - if (getMousePosition(true) != null) return; + if (gameCard.isFaceDown()) { + return; + } + if (getMousePosition(true) != null) { + return; + } if (popupShowing) { synchronized (this) { if (popupShowing) { @@ -764,7 +717,9 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti @Override public void mousePressed(MouseEvent e) { - if (gameCard.isFaceDown()) return; + if (gameCard.isFaceDown()) { + return; + } data.component = this; data.card = this.gameCard; data.gameId = this.gameId; @@ -840,12 +795,10 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti } if (card.getExpansionSetCode() != null && card.getExpansionSetCode().length() > 0) { sb.append("\n").append(card.getCardNumber()).append(" - "); - //sb.append(Sets.getInstance().get(card.getExpansionSetCode()).getName()).append(" - "); sb.append(card.getExpansionSetCode()).append(" - "); sb.append(card.getRarity().toString()); } } -// sb.append("\n").append(card.getId()); return sb.toString(); } @@ -899,7 +852,9 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti @Override public void mouseWheelMoved(MouseWheelEvent e) { - if (gameCard.isFaceDown()) return; + if (gameCard.isFaceDown()) { + return; + } data.component = this; callback.mouseWheelMoved(e, data); } diff --git a/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java b/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java index 363daebc43..c99bf719f2 100644 --- a/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java +++ b/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java @@ -19,13 +19,13 @@ import java.util.regex.Pattern; public class ManaSymbols { private static final Logger log = Logger.getLogger(ManaSymbols.class); - static private final Map manaImages = new HashMap(); - static private final Map manaImagesOriginal = new HashMap(); - static private final Map setImages = new HashMap(); - static private final Map setImagesExist = new HashMap(); - static private Pattern replaceSymbolsPattern = Pattern.compile("\\{([^}/]*)/?([^}]*)\\}"); + private static final Map manaImages = new HashMap(); + private static final Map manaImagesOriginal = new HashMap(); + private static final Map setImages = new HashMap(); + private static final Map setImagesExist = new HashMap(); + private static Pattern replaceSymbolsPattern = Pattern.compile("\\{([^}/]*)/?([^}]*)\\}"); - static public void loadImages() { + public static void loadImages() { String[] symbols = new String[]{"0", "1", "10", "11", "12", "15", "16", "2", "3", "4", "5", "6", "7", "8", "9", "B", "BG", "BR", "G", "GU", "GW", "R", "RG", "RW", "S", "T", "U", "UB", "UR", "W", "WB", "WU", "WP", "UP", "BP", "RP", "GP", "X" /*, "Y", "Z", "slash"*/}; @@ -38,7 +38,6 @@ public class ManaSymbols { manaImages.put(symbol, resized); } catch (Exception e) { log.error("Error for symbol:" + symbol); - //e.printStackTrace(); } file = new File(Constants.RESOURCE_PATH_MANA_MEDIUM + "/" + symbol + ".jpg"); try { @@ -121,11 +120,11 @@ public class ManaSymbols { } } - static public Image getManaSymbolImage(String symbol) { + public static Image getManaSymbolImage(String symbol) { return manaImagesOriginal.get(symbol); } - static public void draw(Graphics g, String manaCost, int x, int y) { + public static void draw(Graphics g, String manaCost, int x, int y) { if (manaCost.length() == 0) { return; } @@ -144,7 +143,7 @@ public class ManaSymbols { } } - static public String getStringManaCost(List manaCost) { + public static String getStringManaCost(List manaCost) { StringBuilder sb = new StringBuilder(); for (String s : manaCost) { sb.append(s); @@ -152,7 +151,7 @@ public class ManaSymbols { return sb.toString().replace("{", "").replace("}", " ").trim(); } - static public int getWidth(String manaCost) { + public static int getWidth(String manaCost) { int width = 0; manaCost = manaCost.replace("\\", ""); StringTokenizer tok = new StringTokenizer(manaCost, " "); @@ -169,7 +168,7 @@ public class ManaSymbols { PAY } - static public synchronized String replaceSymbolsWithHTML(String value, Type type) { + public static synchronized String replaceSymbolsWithHTML(String value, Type type) { value = value.replace("{source}", "|source|"); value = value.replace("{this}", "|this|"); String replaced = value; @@ -189,7 +188,7 @@ public class ManaSymbols { return replaced; } - static public String replaceSetCodeWithHTML(String set, String rarity) { + public static String replaceSetCodeWithHTML(String set, String rarity) { String _set = set; if (_set.equals("CON")) { _set = "CFX"; @@ -203,11 +202,11 @@ public class ManaSymbols { } } - static public Image getSetSymbolImage(String set) { + public static Image getSetSymbolImage(String set) { return setImages.get(set); } - static public BufferedImage getManaSymbolImageSmall(String symbol) { + public static BufferedImage getManaSymbolImageSmall(String symbol) { return manaImages.get(symbol); } } diff --git a/Mage.Client/src/main/java/org/mage/card/arcane/ScaledImagePanel.java b/Mage.Client/src/main/java/org/mage/card/arcane/ScaledImagePanel.java index 776d0f08c5..c12dce237b 100644 --- a/Mage.Client/src/main/java/org/mage/card/arcane/ScaledImagePanel.java +++ b/Mage.Client/src/main/java/org/mage/card/arcane/ScaledImagePanel.java @@ -11,12 +11,10 @@ import javax.swing.JPanel; public class ScaledImagePanel extends JPanel { private static final long serialVersionUID = -1523279873208605664L; public volatile Image srcImage; - //public volatile Image srcImageBlurred; private ScalingType scalingType = ScalingType.bilinear; private boolean scaleLarger; private MultipassType multiPassType = MultipassType.bilinear; - private boolean blur; public ScaledImagePanel () { super(false); @@ -40,10 +38,6 @@ public class ScaledImagePanel extends JPanel { this.scalingType = scalingType; } - public void setScalingBlur (boolean blur) { - this.blur = blur; - } - public void setScaleLarger (boolean scaleLarger) { this.scaleLarger = scaleLarger; } @@ -64,8 +58,9 @@ public class ScaledImagePanel extends JPanel { if (targetWidth > panelWidth) { targetHeight = Math.round(panelWidth * (srcHeight / (float)srcWidth)); targetWidth = panelWidth; - } else + } else { targetHeight = panelHeight; + } } ScalingInfo info = new ScalingInfo(); info.targetWidth = targetWidth; @@ -77,8 +72,11 @@ public class ScaledImagePanel extends JPanel { return info; } + @Override public void paint (Graphics g) { - if (srcImage == null) return; + if (srcImage == null) { + return; + } Graphics2D g2 = (Graphics2D)g.create(); ScalingInfo info = getScalingInfo(); @@ -112,8 +110,12 @@ public class ScaledImagePanel extends JPanel { g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); int tempDestWidth = info.srcWidth / 2, tempDestHeight = info.srcHeight / 2; - if (tempDestWidth < info.targetWidth) tempDestWidth = info.targetWidth; - if (tempDestHeight < info.targetHeight) tempDestHeight = info.targetHeight; + if (tempDestWidth < info.targetWidth) { + tempDestWidth = info.targetWidth; + } + if (tempDestHeight < info.targetHeight) { + tempDestHeight = info.targetHeight; + } Image srcImage = getSourceImage(info); @@ -144,15 +146,21 @@ public class ScaledImagePanel extends JPanel { while (true) { if (tempDestWidth > info.targetWidth) { tempDestWidth = tempDestWidth / 2; - if (tempDestWidth < info.targetWidth) tempDestWidth = info.targetWidth; + if (tempDestWidth < info.targetWidth) { + tempDestWidth = info.targetWidth; + } } if (tempDestHeight > info.targetHeight) { tempDestHeight = tempDestHeight / 2; - if (tempDestHeight < info.targetHeight) tempDestHeight = info.targetHeight; + if (tempDestHeight < info.targetHeight) { + tempDestHeight = info.targetHeight; + } } - if (tempDestWidth == info.targetWidth && tempDestHeight == info.targetHeight) break; + if (tempDestWidth == info.targetWidth && tempDestHeight == info.targetHeight) { + break; + } g2temp.drawImage(tempImage, 0, 0, tempDestWidth, tempDestHeight, 0, 0, tempSrcWidth, tempSrcHeight, null); @@ -168,9 +176,6 @@ public class ScaledImagePanel extends JPanel { private Image getSourceImage (ScalingInfo info) { return srcImage; - //if (!blur || srcImageBlurred == null) return srcImage; - //if (info.srcWidth / 2 < info.targetWidth || info.srcHeight / 2 < info.targetHeight) return srcImage; - //return srcImageBlurred; } public Image getSrcImage() { diff --git a/Mage.Client/src/main/java/org/mage/card/arcane/UI.java b/Mage.Client/src/main/java/org/mage/card/arcane/UI.java index e23d8760a4..6dd0add5d7 100644 --- a/Mage.Client/src/main/java/org/mage/card/arcane/UI.java +++ b/Mage.Client/src/main/java/org/mage/card/arcane/UI.java @@ -17,7 +17,8 @@ import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URL; import java.util.Collections; -import java.util.Hashtable; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import javax.swing.BorderFactory; import javax.swing.ImageIcon; @@ -44,31 +45,31 @@ import javax.swing.text.html.ImageView; * UI utility functions. */ public class UI { - static private Hashtable imageCache = new Hashtable(); + private static ConcurrentMap imageCache = new ConcurrentHashMap(); - static public JToggleButton getToggleButton () { + public static JToggleButton getToggleButton () { JToggleButton button = new JToggleButton(); button.setMargin(new Insets(2, 4, 2, 4)); return button; } - static public JButton getButton () { + public static JButton getButton () { JButton button = new JButton(); button.setMargin(new Insets(2, 4, 2, 4)); return button; } - static public void setTitle (JPanel panel, String title) { + public static void setTitle (JPanel panel, String title) { Border border = panel.getBorder(); if (border instanceof TitledBorder) { - ((TitledBorder)panel.getBorder()).setTitle(title); + ((TitledBorder) panel.getBorder()).setTitle(title); panel.repaint(); - } else + } else { panel.setBorder(BorderFactory.createTitledBorder(title)); + } } - @SuppressWarnings("deprecation") - static public URL getFileURL (String path) { + public static URL getFileURL (String path) { File file = new File(path); if (file.exists()) { try { @@ -79,12 +80,16 @@ public class UI { return UI.class.getResource(path); } - static public ImageIcon getImageIcon (String path) { + public static ImageIcon getImageIcon (String path) { try { InputStream stream; stream = UI.class.getResourceAsStream(path); - if (stream == null && new File(path).exists()) stream = new FileInputStream(path); - if (stream == null) throw new RuntimeException("Image not found: " + path); + if (stream == null && new File(path).exists()) { + stream = new FileInputStream(path); + } + if (stream == null) { + throw new RuntimeException("Image not found: " + path); + } byte[] data = new byte[stream.available()]; stream.read(data); return new ImageIcon(data); @@ -93,27 +98,33 @@ public class UI { } } - static public void setHTMLEditorKit (JEditorPane editorPane) { + public static void setHTMLEditorKit (JEditorPane editorPane) { editorPane.getDocument().putProperty("imageCache", imageCache); // Read internally by ImageView, but never written. - // Extend all this shit to cache images. + editorPane.setEditorKit(new HTMLEditorKit() { private static final long serialVersionUID = -54602188235105448L; + @Override public ViewFactory getViewFactory () { return new HTMLFactory() { - public View create (Element elem) { + @Override + public View create(Element elem) { Object o = elem.getAttributes().getAttribute(StyleConstants.NameAttribute); if (o instanceof HTML.Tag) { - HTML.Tag kind = (HTML.Tag)o; - if (kind == HTML.Tag.IMG) return new ImageView(elem) { - public URL getImageURL () { - URL url = super.getImageURL(); - // Put an image into the cache to be read by other ImageView methods. - if (url != null && imageCache.get(url) == null) - imageCache.put(url, Toolkit.getDefaultToolkit().createImage(url)); - return url; - } - }; + HTML.Tag kind = (HTML.Tag) o; + if (kind == HTML.Tag.IMG) { + return new ImageView(elem) { + @Override + public URL getImageURL() { + URL url = super.getImageURL(); + // Put an image into the cache to be read by other ImageView methods. + if (url != null && imageCache.get(url) == null) { + imageCache.put(url, Toolkit.getDefaultToolkit().createImage(url)); + } + return url; + } + }; + } } return super.create(elem); } @@ -122,10 +133,11 @@ public class UI { }); } - static public void setVerticalScrollingView (JScrollPane scrollPane, final Component view) { + public static void setVerticalScrollingView (JScrollPane scrollPane, final Component view) { final JViewport viewport = new JViewport(); viewport.setLayout(new ViewportLayout() { private static final long serialVersionUID = 7701568740313788935L; + @Override public void layoutContainer (Container parent) { viewport.setViewPosition(new Point(0, 0)); Dimension viewportSize = viewport.getSize(); @@ -139,19 +151,21 @@ public class UI { scrollPane.setViewport(viewport); } - static public String getDisplayManaCost (String manaCost) { + public static String getDisplayManaCost (String manaCost) { manaCost = manaCost.replace("/", ""); // A pipe in the cost means "process left of the pipe as the card color, but display right of the pipe as the cost". int pipePosition = manaCost.indexOf("{|}"); - if (pipePosition != -1) manaCost = manaCost.substring(pipePosition + 3); + if (pipePosition != -1) { + manaCost = manaCost.substring(pipePosition + 3); + } return manaCost; } - static public void invokeLater (Runnable runnable) { + public static void invokeLater (Runnable runnable) { EventQueue.invokeLater(runnable); } - static public void invokeAndWait (Runnable runnable) { + public static void invokeAndWait (Runnable runnable) { if (EventQueue.isDispatchThread()) { runnable.run(); return; @@ -164,7 +178,7 @@ public class UI { } } - static public void setSystemLookAndFeel () { + public static void setSystemLookAndFeel () { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception ex) { @@ -173,10 +187,12 @@ public class UI { } } - static public void setDefaultFont (Font font) { + public static void setDefaultFont (Font font) { for (Object key : Collections.list(UIManager.getDefaults().keys())) { Object value = UIManager.get(key); - if (value instanceof javax.swing.plaf.FontUIResource) UIManager.put(key, font); + if (value instanceof javax.swing.plaf.FontUIResource) { + UIManager.put(key, font); + } } } } diff --git a/Mage.Client/src/main/java/org/mage/card/arcane/Util.java b/Mage.Client/src/main/java/org/mage/card/arcane/Util.java index a8413f573d..e9abd99cac 100644 --- a/Mage.Client/src/main/java/org/mage/card/arcane/Util.java +++ b/Mage.Client/src/main/java/org/mage/card/arcane/Util.java @@ -18,10 +18,10 @@ import javax.swing.SwingUtilities; @SuppressWarnings({ "rawtypes", "unchecked" }) public class Util { - static public final boolean isMac = System.getProperty("os.name").toLowerCase().indexOf("mac") != -1; - static public final boolean isWindows = System.getProperty("os.name").toLowerCase().indexOf("windows") == -1; + public static final boolean isMac = System.getProperty("os.name").toLowerCase().indexOf("mac") != -1; + public static final boolean isWindows = System.getProperty("os.name").toLowerCase().indexOf("windows") == -1; - static public Robot robot; + public static Robot robot; static { try { new Robot(); @@ -30,10 +30,11 @@ public class Util { } } - static public ThreadPoolExecutor threadPool; + public static final ThreadPoolExecutor threadPool; static private int threadCount; static { threadPool = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new ThreadFactory() { + @Override public Thread newThread (Runnable runnable) { threadCount++; Thread thread = new Thread(runnable, "Util" + threadCount); @@ -54,7 +55,9 @@ public class Util { throws IOException { for (NetworkInterface iface : Collections.list(ifaces)) { for (InetAddress address : Collections.list(iface.getInetAddresses())) { - if (!address.isSiteLocalAddress()) continue; + if (!address.isSiteLocalAddress()) { + continue; + } // Java 1.5 doesn't support getting the subnet mask, so try the two most common. byte[] ip = address.getAddress(); ip[3] = -1; // 255.255.255.0 @@ -65,14 +68,14 @@ public class Util { } } - static public void sleep (int millis) { + public static void sleep (int millis) { try { Thread.sleep(millis); } catch (InterruptedException ignored) { } } - static public boolean classExists (String className) { + public static boolean classExists (String className) { try { Class.forName(className); return true; @@ -81,7 +84,7 @@ public class Util { } } - static public void wait (Object lock) { + public static void wait (Object lock) { synchronized (lock) { try { lock.wait(); diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/CardPluginImpl.java b/Mage.Client/src/main/java/org/mage/plugins/card/CardPluginImpl.java index 9deac148ea..1b993691e0 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/CardPluginImpl.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/CardPluginImpl.java @@ -43,15 +43,14 @@ import java.util.List; @Author(name = "nantuko") public class CardPluginImpl implements CardPlugin { - private final static Logger log = Logger.getLogger(CardPluginImpl.class); + private static final Logger log = Logger.getLogger(CardPluginImpl.class); - static private final int GUTTER_Y = 15; - static private final int GUTTER_X = 5; + private static final int GUTTER_Y = 15; + private static final int GUTTER_X = 5; static final float EXTRA_CARD_SPACING_X = 0.04f; - static private final float CARD_SPACING_Y = 0.03f; - static private final float STACK_SPACING_X = 0.07f; - static private final float STACK_SPACING_Y = 0.13f; - //static private final int MW_GUIDE_HEIGHT = 30; + private static final float CARD_SPACING_Y = 0.03f; + private static final float STACK_SPACING_X = 0.07f; + private static final float STACK_SPACING_Y = 0.13f; private int landStackMax = 5; private int cardWidthMin = 50, cardWidthMax = Constants.CARD_SIZE_FULL.width; @@ -69,8 +68,6 @@ public class CardPluginImpl implements CardPlugin { @PluginLoaded public void newPlugin(CardPlugin plugin) { - //ManaSymbols.loadImages(); - //log.info(plugin.toString() + " has been loaded."); } @Override @@ -104,21 +101,16 @@ public class CardPluginImpl implements CardPlugin { //requires to find out is position have been changed that includes: //adding/removing permanents, type change - if (ui == null) + if (ui == null) { throw new RuntimeException("Error: no components"); - //JComponent component = ui.get("jScrollPane"); - JComponent component2 = ui.get("battlefieldPanel"); - //if (component == null) - //throw new RuntimeException("Error: jScrollPane is missing"); - if (component2 == null) - throw new RuntimeException("Error: battlefieldPanel is missing"); - //if (!(component instanceof JScrollPane)) - //throw new RuntimeException("Error: jScrollPane has wrong type."); - //if (!(component instanceof JScrollPane)) - //throw new RuntimeException("Error: battlefieldPanel is missing"); + } + JComponent component = ui.get("battlefieldPanel"); - //JScrollPane jScrollPane = (JScrollPane) component; - JLayeredPane battlefieldPanel = (JLayeredPane) component2; + if (component == null) { + throw new RuntimeException("Error: battlefieldPanel is missing"); + } + + JLayeredPane battlefieldPanel = (JLayeredPane) component; JComponent jPanel = ui.get("jPanel"); Row allLands = new Row(); @@ -126,8 +118,9 @@ public class CardPluginImpl implements CardPlugin { outerLoop: // for (MagePermanent permanent : permanents) { - if (!CardUtil.isLand(permanent) || CardUtil.isCreature(permanent)) + if (!CardUtil.isLand(permanent) || CardUtil.isCreature(permanent)) { continue; + } int insertIndex = -1; @@ -150,8 +143,9 @@ public class CardPluginImpl implements CardPlugin { stack.add(0, permanent); continue outerLoop; } - if (insertIndex != -1) + if (insertIndex != -1) { break; + } } Stack stack = new Stack(); @@ -178,11 +172,11 @@ public class CardPluginImpl implements CardPlugin { while (true) { rows.clear(); cardHeight = Math.round(cardWidth * CardPanel.ASPECT_RATIO); - extraCardSpacingX = (int) Math.round(cardWidth * EXTRA_CARD_SPACING_X); + extraCardSpacingX = Math.round(cardWidth * EXTRA_CARD_SPACING_X); cardSpacingX = cardHeight - cardWidth + extraCardSpacingX; - cardSpacingY = (int) Math.round(cardHeight * CARD_SPACING_Y); - stackSpacingX = stackVertical ? 0 : (int) Math.round(cardWidth * STACK_SPACING_X); - stackSpacingY = (int) Math.round(cardHeight * STACK_SPACING_Y); + cardSpacingY = Math.round(cardHeight * CARD_SPACING_Y); + stackSpacingX = stackVertical ? 0 : Math.round(cardWidth * STACK_SPACING_X); + stackSpacingY = Math.round(cardHeight * STACK_SPACING_Y); Row creatures = (Row) allCreatures.clone(); Row lands = (Row) allLands.clone(); Row others = (Row) allOthers.clone(); @@ -192,26 +186,30 @@ public class CardPluginImpl implements CardPlugin { wrap(lands, rows, afterCreaturesIndex); // Store the current rows and others. List storedRows = new ArrayList(rows.size()); - for (Row row : rows) + for (Row row : rows) { storedRows.add((Row) row.clone()); + } Row storedOthers = (Row) others.clone(); // Fill in all rows with others. - for (Row row : rows) + for (Row row : rows) { fillRow(others, rows, row); + } // Stop if everything fits, otherwise revert back to the stored values. - if (creatures.isEmpty() && lands.isEmpty() && others.isEmpty()) + if (creatures.isEmpty() && lands.isEmpty() && others.isEmpty()) { break; + } rows = storedRows; others = storedOthers; // Try to put others on their own row(s) and fill in the rest. wrap(others, rows, afterCreaturesIndex); - for (Row row : rows) + for (Row row : rows) { fillRow(others, rows, row); + } // If that still doesn't fit, scale down. - if (creatures.isEmpty() && lands.isEmpty() && others.isEmpty()) + if (creatures.isEmpty() && lands.isEmpty() && others.isEmpty()) { break; - //cardWidth = (int)(cardWidth / 1.2); + } //FIXME: -1 is too slow. why not binary search? cardWidth -= 3; } @@ -232,7 +230,6 @@ public class CardPluginImpl implements CardPlugin { } // Position all card panels. - x = 0; y = GUTTER_Y; for (Row row : rows) { int rowBottom = 0; @@ -242,17 +239,18 @@ public class CardPluginImpl implements CardPlugin { // Align others to the right. if (othersOnTheRight && RowType.other.isType(stack.get(0))) { x = playAreaWidth - GUTTER_X + extraCardSpacingX; - for (int i = stackIndex, n = row.size(); i < n; i++) + for (int i = stackIndex, n = row.size(); i < n; i++) { x -= row.get(i).getWidth(); + } } for (int panelIndex = 0, panelCount = stack.size(); panelIndex < panelCount; panelIndex++) { MagePermanent panel = stack.get(panelIndex); int stackPosition = panelCount - panelIndex - 1; - if (jPanel != null) + if (jPanel != null) { jPanel.setComponentZOrder(panel, panelIndex); + } int panelX = x + (stackPosition * stackSpacingX); int panelY = y + (stackPosition * stackSpacingY); - //panel.setLocation(panelX, panelY); try { // may cause: // java.lang.IllegalArgumentException: illegal component position 26 should be less then 26 @@ -286,10 +284,12 @@ public class CardPluginImpl implements CardPlugin { int rowWidth = currentRow.getWidth(); if (!currentRow.isEmpty() && rowWidth + stack.getWidth() > playAreaWidth) { // Stop processing if the row is too wide or tall. - if (!allowHeightOverflow && rowWidth > playAreaWidth) + if (!allowHeightOverflow && rowWidth > playAreaWidth) { break; - if (!allowHeightOverflow && getRowsHeight(rows) + sourceRow.getHeight() > playAreaHeight) + } + if (!allowHeightOverflow && getRowsHeight(rows) + sourceRow.getHeight() > playAreaHeight) { break; + } rows.add(insertIndex == -1 ? rows.size() : insertIndex, currentRow); currentRow = new Row(); } @@ -305,9 +305,11 @@ public class CardPluginImpl implements CardPlugin { } } // Remove the wrapped stacks from the source row. - for (Row row : rows) - for (Stack stack : row) + for (Row row : rows) { + for (Stack stack : row) { sourceRow.remove(stack); + } + } return insertIndex; } @@ -316,11 +318,12 @@ public class CardPluginImpl implements CardPlugin { while (!sourceRow.isEmpty()) { Stack stack = sourceRow.get(0); rowWidth += stack.getWidth(); - if (rowWidth > playAreaWidth) + if (rowWidth > playAreaWidth) { + break; + } + if (stack.getHeight() > row.getHeight() + && getRowsHeight(rows) - row.getHeight() + stack.getHeight() > playAreaHeight) { break; - if (stack.getHeight() > row.getHeight()) { - if (getRowsHeight(rows) - row.getHeight() + stack.getHeight() > playAreaHeight) - break; } row.add(sourceRow.remove(0)); } @@ -328,12 +331,13 @@ public class CardPluginImpl implements CardPlugin { private int getRowsHeight(List rows) { int height = 0; - for (Row row : rows) + for (Row row : rows) { height += row.getHeight(); + } return height - cardSpacingY + GUTTER_Y * 2; } - static private enum RowType { + private static enum RowType { land, creature, other; public boolean isType(MagePermanent card) { @@ -351,6 +355,8 @@ public class CardPluginImpl implements CardPlugin { } private class Row extends ArrayList { + private static final long serialVersionUID = 1L; + public Row() { super(16); } @@ -379,37 +385,35 @@ public class CardPluginImpl implements CardPlugin { } private int getWidth() { - if (isEmpty()) + if (isEmpty()) { return 0; + } int width = 0; - for (Stack stack : this) + for (Stack stack : this) { width += stack.getWidth(); + } return width + GUTTER_X * 2 - extraCardSpacingX; } private int getHeight() { - if (isEmpty()) + if (isEmpty()) { return 0; + } int height = 0; - for (Stack stack : this) + for (Stack stack : this) { height = Math.max(height, stack.getHeight()); + } return height; } } private class Stack extends ArrayList { + private static final long serialVersionUID = 1L; + public Stack() { super(8); } - @Override - public boolean add(MagePermanent panel) { - boolean appended = super.add(panel); - //for (CardPanel attachedPanel : panel.attachedPanels) - //add(attachedPanel); - return appended; - } - private int getWidth() { return cardWidth + (size() - 1) * stackSpacingX + cardSpacingX; } diff --git a/Mage.Common/src/mage/cards/MageCard.java b/Mage.Common/src/mage/cards/MageCard.java index 7c13249d41..c2012ad262 100644 --- a/Mage.Common/src/mage/cards/MageCard.java +++ b/Mage.Common/src/mage/cards/MageCard.java @@ -3,31 +3,31 @@ package mage.cards; import mage.cards.action.ActionCallback; import mage.view.CardView; -import javax.swing.*; -import java.awt.*; +import java.awt.Image; import java.util.UUID; +import javax.swing.JPanel; public abstract class MageCard extends JPanel { private static final long serialVersionUID = 6089945326434301879L; - abstract public void onBeginAnimation(); - abstract public void onEndAnimation(); - abstract public boolean isTapped(); - abstract public boolean isFlipped(); - abstract public void setAlpha(float transparency); - abstract public float getAlpha(); - abstract public CardView getOriginal(); - abstract public void setCardBounds(int x, int y, int width, int height); - abstract public void update(CardView card); - abstract public void updateImage(); - abstract public Image getImage(); - abstract public void setFoil(boolean foil); - abstract public boolean isFoil(); - abstract public void setZone(String zone); - abstract public String getZone(); - abstract public void updateCallback(ActionCallback callback, UUID gameId); - abstract public void toggleTransformed(); - abstract public boolean isTransformed(); - abstract public void showCardTitle(); - abstract public void setSelected(boolean selected); + public abstract void onBeginAnimation(); + public abstract void onEndAnimation(); + public abstract boolean isTapped(); + public abstract boolean isFlipped(); + public abstract void setAlpha(float transparency); + public abstract float getAlpha(); + public abstract CardView getOriginal(); + public abstract void setCardBounds(int x, int y, int width, int height); + public abstract void update(CardView card); + public abstract void updateImage(); + public abstract Image getImage(); + public abstract void setFoil(boolean foil); + public abstract boolean isFoil(); + public abstract void setZone(String zone); + public abstract String getZone(); + public abstract void updateCallback(ActionCallback callback, UUID gameId); + public abstract void toggleTransformed(); + public abstract boolean isTransformed(); + public abstract void showCardTitle(); + public abstract void setSelected(boolean selected); } diff --git a/Mage.Common/src/mage/cards/MagePermanent.java b/Mage.Common/src/mage/cards/MagePermanent.java index eb8e691c28..59dd9eef17 100644 --- a/Mage.Common/src/mage/cards/MagePermanent.java +++ b/Mage.Common/src/mage/cards/MagePermanent.java @@ -6,7 +6,7 @@ import mage.view.PermanentView; public abstract class MagePermanent extends MageCard { private static final long serialVersionUID = -3469258620601702171L; - abstract public List getLinks(); - abstract public void update(PermanentView card); - abstract public PermanentView getOriginalPermanent(); + public abstract List getLinks(); + public abstract void update(PermanentView card); + public abstract PermanentView getOriginalPermanent(); } diff --git a/Mage.Common/src/mage/utils/ThreadUtils.java b/Mage.Common/src/mage/utils/ThreadUtils.java index f6520857e0..5d365a2706 100644 --- a/Mage.Common/src/mage/utils/ThreadUtils.java +++ b/Mage.Common/src/mage/utils/ThreadUtils.java @@ -12,11 +12,12 @@ import java.util.concurrent.TimeUnit; */ public class ThreadUtils { - static public ThreadPoolExecutor threadPool; - static public ThreadPoolExecutor threadPool2; - static private int threadCount; + public static final ThreadPoolExecutor threadPool; + public static final ThreadPoolExecutor threadPool2; + private static int threadCount; static { threadPool = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new ThreadFactory() { + @Override public Thread newThread (Runnable runnable) { threadCount++; Thread thread = new Thread(runnable, "Util" + threadCount); @@ -26,6 +27,7 @@ public class ThreadUtils { }); threadPool.prestartAllCoreThreads(); threadPool2 = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new ThreadFactory() { + @Override public Thread newThread (Runnable runnable) { threadCount++; Thread thread = new Thread(runnable, "TP2" + threadCount); @@ -36,14 +38,14 @@ public class ThreadUtils { threadPool2.prestartAllCoreThreads(); } - static public void sleep (int millis) { + public static void sleep (int millis) { try { Thread.sleep(millis); } catch (InterruptedException ignored) { } } - static public void wait (Object lock) { + public static void wait (Object lock) { synchronized (lock) { try { lock.wait(); diff --git a/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java b/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java index 170b25b85b..ec2c355d90 100644 --- a/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java +++ b/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java @@ -52,7 +52,7 @@ import org.apache.log4j.Logger; */ public class ConsolePanel extends javax.swing.JPanel { - private final static Logger logger = Logger.getLogger(ConsolePanel.class); + private static final Logger logger = Logger.getLogger(ConsolePanel.class); private TableUserModel tableUserModel; private TableTableModel tableTableModel; @@ -400,7 +400,7 @@ class UpdateUsersTask extends SwingWorker> { private Session session; private ConsolePanel panel; - private final static Logger logger = Logger.getLogger(UpdateUsersTask.class); + private static final Logger logger = Logger.getLogger(UpdateUsersTask.class); UpdateUsersTask(Session session, ConsolePanel panel) { this.session = session; @@ -439,7 +439,7 @@ class UpdateTablesTask extends SwingWorker> { private UUID roomId; private ConsolePanel panel; - private final static Logger logger = Logger.getLogger(UpdateTablesTask.class); + private static final Logger logger = Logger.getLogger(UpdateTablesTask.class); UpdateTablesTask(Session session, UUID roomId, ConsolePanel panel) { this.session = session; diff --git a/Mage.Sets/src/mage/sets/darkascension/Immerwolf.java b/Mage.Sets/src/mage/sets/darkascension/Immerwolf.java index 88d75da756..8a9036febc 100644 --- a/Mage.Sets/src/mage/sets/darkascension/Immerwolf.java +++ b/Mage.Sets/src/mage/sets/darkascension/Immerwolf.java @@ -52,7 +52,7 @@ import java.util.UUID; */ public class Immerwolf extends CardImpl { - private final static FilterCreaturePermanent filter = new FilterCreaturePermanent("Wolf and Werewolf creatures"); + private static final FilterCreaturePermanent filter = new FilterCreaturePermanent("Wolf and Werewolf creatures"); static { filter.add(Predicates.or(new SubtypePredicate("Wolf"), new SubtypePredicate("Werewolf"))); @@ -90,8 +90,8 @@ public class Immerwolf extends CardImpl { class ImmerwolfEffect extends ReplacementEffectImpl { - private final static FilterCreaturePermanent filterWerewolf = new FilterCreaturePermanent("Werewolf creature"); - private final static FilterCreaturePermanent filterNonhuman = new FilterCreaturePermanent("Non-human creature"); + private static final FilterCreaturePermanent filterWerewolf = new FilterCreaturePermanent("Werewolf creature"); + private static final FilterCreaturePermanent filterNonhuman = new FilterCreaturePermanent("Non-human creature"); static { filterWerewolf.add(new SubtypePredicate("Werewolf")); diff --git a/Mage.Sets/src/mage/sets/eventide/BalefireLiege.java b/Mage.Sets/src/mage/sets/eventide/BalefireLiege.java index 60aec5c0e7..8e87d83520 100644 --- a/Mage.Sets/src/mage/sets/eventide/BalefireLiege.java +++ b/Mage.Sets/src/mage/sets/eventide/BalefireLiege.java @@ -50,10 +50,10 @@ import mage.target.TargetPlayer; */ public class BalefireLiege extends CardImpl { - private final static FilterCreaturePermanent filterRedCreature = new FilterCreaturePermanent("red creatures"); - private final static FilterCreaturePermanent filterWhiteCreature = new FilterCreaturePermanent("white creatures"); - private final static FilterSpell filterRedSpell = new FilterSpell("a red spell"); - private final static FilterSpell filterWhiteSpell = new FilterSpell("a white spell"); + private static final FilterCreaturePermanent filterRedCreature = new FilterCreaturePermanent("red creatures"); + private static final FilterCreaturePermanent filterWhiteCreature = new FilterCreaturePermanent("white creatures"); + private static final FilterSpell filterRedSpell = new FilterSpell("a red spell"); + private static final FilterSpell filterWhiteSpell = new FilterSpell("a white spell"); static { filterRedCreature.add(new ColorPredicate(ObjectColor.RED)); diff --git a/Mage.Sets/src/mage/sets/eventide/DeathbringerLiege.java b/Mage.Sets/src/mage/sets/eventide/DeathbringerLiege.java index a285ab1554..c7fceed1b7 100644 --- a/Mage.Sets/src/mage/sets/eventide/DeathbringerLiege.java +++ b/Mage.Sets/src/mage/sets/eventide/DeathbringerLiege.java @@ -53,10 +53,10 @@ import mage.target.common.TargetCreaturePermanent; * @author Loki */ public class DeathbringerLiege extends CardImpl { - private final static FilterCreaturePermanent filterWhite = new FilterCreaturePermanent("white creatures"); - private final static FilterCreaturePermanent filterBlack = new FilterCreaturePermanent("black creatures"); - private final static FilterSpell filterWhiteSpellCard = new FilterSpell("a white spell"); - private final static FilterSpell filterBlackSpellCard = new FilterSpell("a black spell"); + private static final FilterCreaturePermanent filterWhite = new FilterCreaturePermanent("white creatures"); + private static final FilterCreaturePermanent filterBlack = new FilterCreaturePermanent("black creatures"); + private static final FilterSpell filterWhiteSpellCard = new FilterSpell("a white spell"); + private static final FilterSpell filterBlackSpellCard = new FilterSpell("a black spell"); static { filterWhite.add(new ColorPredicate(ObjectColor.WHITE)); diff --git a/Mage.Sets/src/mage/sets/guildpact/TiborAndLumia.java b/Mage.Sets/src/mage/sets/guildpact/TiborAndLumia.java index 9769243b36..90d7e82ef6 100644 --- a/Mage.Sets/src/mage/sets/guildpact/TiborAndLumia.java +++ b/Mage.Sets/src/mage/sets/guildpact/TiborAndLumia.java @@ -52,9 +52,9 @@ import mage.target.common.TargetCreaturePermanent; */ public class TiborAndLumia extends CardImpl { - private final static FilterSpell filterBlue = new FilterSpell("a blue spell"); - private final static FilterCreaturePermanent filter = new FilterCreaturePermanent("creature without flying"); - private final static FilterSpell filterRed = new FilterSpell("a red spell"); + private static final FilterSpell filterBlue = new FilterSpell("a blue spell"); + private static final FilterCreaturePermanent filter = new FilterCreaturePermanent("creature without flying"); + private static final FilterSpell filterRed = new FilterSpell("a red spell"); static { filterBlue.add(new ColorPredicate(ObjectColor.BLUE));