From 2b18a8ec42ddc282bb176f25944983493ff37dd7 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 2 Feb 2016 12:38:54 +0100 Subject: [client] Add icons to lecture and image list buttons --- .../src/main/java/org/openslx/dozmod/gui/Gui.java | 17 ++++++++++ .../window/layout/ImageDetailsWindowLayout.java | 4 ++- .../gui/window/layout/ImageListWindowLayout.java | 29 ++++++++++------- .../window/layout/LectureDetailsWindowLayout.java | 3 +- .../gui/window/layout/LectureListWindowLayout.java | 16 +++++---- .../java/org/openslx/dozmod/gui/wizard/Wizard.java | 33 ++----------------- .../org/openslx/dozmod/util/ResourceLoader.java | 38 ++++++++++++++++++++++ 7 files changed, 88 insertions(+), 52 deletions(-) (limited to 'dozentenmodul/src/main/java/org') diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java index 66e2fa60..dd9d8dc7 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java @@ -15,6 +15,7 @@ import java.lang.reflect.InvocationTargetException; import java.util.concurrent.atomic.AtomicReference; import javax.management.monitor.Monitor; +import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; @@ -22,6 +23,7 @@ import javax.swing.SwingUtilities; import org.apache.log4j.Logger; import org.openslx.dozmod.Config; import org.openslx.dozmod.gui.helper.MessageType; +import org.openslx.dozmod.util.ResourceLoader; import org.openslx.util.QuickTimer; public class Gui { @@ -119,6 +121,21 @@ public class Gui { int scale = Config.getFontScaling(); return new Dimension(width * scale / 100, height * scale / 100); } + + /** + * Load given icon resource, optionally scaling it while taking + * the user's zoom factor into account. + * + * @param path resource path + * @param description resource description + * @param maxHeight maximum height of image, which it will be scaled to + * @param context context component, for alpha blending + * @return scaled icon, if too large, otherwise the unmodified icon + */ + public static Icon getScaledIconResource(String path, String description, int maxHeight, Component context) { + int height = maxHeight * Config.getFontScaling() / 100; + return ResourceLoader.getIcon(path, description, height, context); + } /** * Get the {@link GraphicsDevice} which the given {@link Point} lies in. diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageDetailsWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageDetailsWindowLayout.java index c572f5ec..de120a81 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageDetailsWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageDetailsWindowLayout.java @@ -35,6 +35,8 @@ import org.openslx.thrifthelper.Comparators; @SuppressWarnings("serial") public abstract class ImageDetailsWindowLayout extends JDialog { + + private static final int ICON_SIZE_Y = 24; protected final JTextField txtTitle; protected final JTextArea txtDescription; @@ -204,7 +206,7 @@ public abstract class ImageDetailsWindowLayout extends JDialog { buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS)); btnSaveChanges = new JButton("Speichern"); btnClose = new JButton("Schließen"); - btnUpdateImage = new JButton("Neue Image-Version hochladen"); + btnUpdateImage = new JButton("Neue Image-Version hochladen", Gui.getScaledIconResource("/img/upload-icon.png", "New VM", ICON_SIZE_Y, buttonPanel)); buttonPanel.add(btnUpdateImage); buttonPanel.add(Box.createGlue()); // user feedback slot diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java index 1fa98797..f401dba9 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java @@ -1,16 +1,19 @@ package org.openslx.dozmod.gui.window.layout; import java.awt.BorderLayout; -import java.awt.Dimension; import java.awt.Font; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; +import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JPanel; +import javax.swing.JSeparator; +import javax.swing.SwingConstants; import org.apache.log4j.Logger; +import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.control.ImageListViewer; import org.openslx.dozmod.gui.control.ImageListViewer.FilterType; import org.openslx.dozmod.gui.control.QLabel; @@ -21,12 +24,14 @@ import org.openslx.dozmod.gui.helper.GridManager; public abstract class ImageListWindowLayout extends CompositePage { private final static Logger LOGGER = Logger.getLogger(ImageListWindowLayout.class); + + private static final int ICON_SIZE_Y = 24; protected final static String infoTextString = "Hier können Sie Virtuelle Maschinen hochladen, herunterladen, bearbeiten und löschen."; protected final static String infoTitleString = "Übersicht Virtuelle Maschinen"; protected final static String newButtonLabel = "Neue VM"; protected final static String newLectureButtonLabel = "Neue Veranstaltung"; - protected final static String editButtonLabel = "Detailansicht"; + protected final static String editButtonLabel = "Detailansicht / Bearbeiten"; protected final static String downloadButtonLabel = "Download"; protected final static String deleteButtonLabel = "Löschen"; protected final static String switchViewButtonLabel = "Zu 'Veranstaltungen' wechseln"; @@ -61,19 +66,19 @@ public abstract class ImageListWindowLayout extends CompositePage { JPanel buttonPanel = new JPanel(); buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS)); - btnNewVm = new JButton(newButtonLabel); - btnNewLecture = new JButton(newLectureButtonLabel); - btnDelete = new JButton(deleteButtonLabel); - btnEditDetails = new JButton(editButtonLabel); - btnDownload = new JButton(downloadButtonLabel); - btnSwitchView = new JButton(switchViewButtonLabel); + btnNewVm = new JButton(newButtonLabel, Gui.getScaledIconResource("/img/new-icon.png", "New VM", ICON_SIZE_Y, buttonPanel)); + btnEditDetails = new JButton(editButtonLabel, Gui.getScaledIconResource("/img/edit-icon.png", "Edit VM", ICON_SIZE_Y, buttonPanel)); + btnDelete = new JButton(deleteButtonLabel, Gui.getScaledIconResource("/img/red-cross.png", "Delete VM", ICON_SIZE_Y, buttonPanel)); + btnDownload = new JButton(downloadButtonLabel, Gui.getScaledIconResource("/img/download-icon.png", "New VM", ICON_SIZE_Y, buttonPanel)); + btnNewLecture = new JButton(newLectureButtonLabel, Gui.getScaledIconResource("/img/new-lecture-icon.png", "New Lecture", ICON_SIZE_Y, buttonPanel)); + btnSwitchView = new JButton(switchViewButtonLabel, Gui.getScaledIconResource("/img/switch-icon.png", "Switch", ICON_SIZE_Y, buttonPanel)); buttonPanel.add(btnNewVm); - buttonPanel.add(Box.createRigidArea(new Dimension(5, 0))); - buttonPanel.add(btnNewLecture); buttonPanel.add(btnEditDetails); - buttonPanel.add(btnDownload); - buttonPanel.add(Box.createRigidArea(new Dimension(5, 0))); buttonPanel.add(btnDelete); + buttonPanel.add(Box.createHorizontalStrut(5)); + buttonPanel.add(btnDownload); + buttonPanel.add(Box.createHorizontalStrut(5)); + buttonPanel.add(btnNewLecture); buttonPanel.add(Box.createHorizontalGlue()); buttonPanel.add(btnSwitchView); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureDetailsWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureDetailsWindowLayout.java index 8d751a31..913a26e6 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureDetailsWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureDetailsWindowLayout.java @@ -5,6 +5,7 @@ import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.GridBagConstraints; +import java.awt.Insets; import java.util.Calendar; import java.util.Date; import java.util.Properties; @@ -95,7 +96,7 @@ public abstract class LectureDetailsWindowLayout extends JDialog { // then we will set the panel in BorderLayout.CENTER JPanel infoPanel = new JPanel(); infoPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); - GridManager grid = new GridManager(infoPanel, 3); + GridManager grid = new GridManager(infoPanel, 3, true, new Insets(2, 2, 2, 2)); // -- name -- txtTitle = new JTextArea(); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureListWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureListWindowLayout.java index 7aee0f30..a2c1e46a 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureListWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureListWindowLayout.java @@ -16,6 +16,7 @@ import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.border.TitledBorder; +import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.control.QLabel; import org.openslx.dozmod.gui.control.table.LectureTable; import org.openslx.dozmod.gui.control.table.QScrollPane; @@ -25,10 +26,12 @@ import org.openslx.dozmod.gui.window.LectureListWindow.FilterType; @SuppressWarnings("serial") public abstract class LectureListWindowLayout extends CompositePage { + + private static final int ICON_SIZE_Y = 24; private static final String infoTitleString = "Übersicht Veranstaltungen"; private static final String newButtonLabel = "Neue Veranstaltung"; - private static final String editButtonLabel = "Detailansicht"; + private static final String editButtonLabel = "Detailansicht / Bearbeiten"; private static final String deleteButtonLabel = "Löschen"; private static final String switchViewButtonLabel = "Zu 'Images' wechseln"; private static final String filterPanelLabel = "Suchen"; @@ -102,16 +105,15 @@ public abstract class LectureListWindowLayout extends CompositePage { buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS)); buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); - btnNewLecture = new JButton(newButtonLabel); + btnNewLecture = new JButton(newButtonLabel, Gui.getScaledIconResource("/img/new-lecture-icon.png", "New Lecture", ICON_SIZE_Y, buttonPanel)); + btnEditLecture = new JButton(editButtonLabel, Gui.getScaledIconResource("/img/edit-icon.png", "Edit Lecture", ICON_SIZE_Y, buttonPanel)); + btnDeleteLecture = new JButton(deleteButtonLabel, Gui.getScaledIconResource("/img/red-cross.png", "Delete Lecture", ICON_SIZE_Y, buttonPanel)); + btnSwitchView = new JButton(switchViewButtonLabel, Gui.getScaledIconResource("/img/switch-icon.png", "Switch", ICON_SIZE_Y, buttonPanel)); buttonPanel.add(btnNewLecture); - buttonPanel.add(Box.createRigidArea(new Dimension(5, 0))); - btnEditLecture = new JButton(editButtonLabel); buttonPanel.add(btnEditLecture); - buttonPanel.add(Box.createRigidArea(new Dimension(5, 0))); - btnDeleteLecture = new JButton(deleteButtonLabel); + buttonPanel.add(Box.createHorizontalStrut(5)); buttonPanel.add(btnDeleteLecture); buttonPanel.add(Box.createHorizontalGlue()); - btnSwitchView = new JButton(switchViewButtonLabel); buttonPanel.add(btnSwitchView); // ----------------- end group for table ------------------------- diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/Wizard.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/Wizard.java index 20cb1436..244181a2 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/Wizard.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/Wizard.java @@ -4,23 +4,17 @@ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; -import java.awt.Graphics2D; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.awt.image.BufferedImage; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; -import javax.swing.Icon; -import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; @@ -31,6 +25,7 @@ import javax.swing.SwingConstants; import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.control.QLabel; import org.openslx.dozmod.gui.helper.GridManager; +import org.openslx.dozmod.util.ResourceLoader; @SuppressWarnings("serial") public abstract class Wizard extends JDialog { @@ -177,36 +172,12 @@ public abstract class Wizard extends JDialog { pageDesc = ""; titleLabel.setText(pageTitle); messageLabel.setText(pageDesc); - messageLabel.setIcon(getScaledIcon(page.getMessageIcon())); + messageLabel.setIcon(ResourceLoader.getScaledIcon(page.getMessageIcon(), messageLabel.getHeight(), messageLabel)); messageLabel.setForeground(page.getMessageColor()); messageLabel.validate(); setTitle(getWindowTitle() + " - " + pageTitle); } - private static final Map iconCache = new HashMap<>(); - - private Icon getScaledIcon(Icon icon) { - if (icon == null) - return null; - ImageIcon cached = iconCache.get(icon); - if (cached != null && cached.getIconHeight() == messageLabel.getHeight()) - return cached; - // Generate? - float iHeight = icon.getIconHeight(); - float tHeight = messageLabel.getHeight(); - if (iHeight <= tHeight) - return icon; // Small enough - // Scale down: - BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), - BufferedImage.TYPE_INT_ARGB_PRE); - Graphics2D g = image.createGraphics(); - icon.paintIcon(messageLabel, g, 0, 0); - ImageIcon scaledIcon = new ImageIcon(image.getScaledInstance( - (int) (icon.getIconWidth() * (tHeight / iHeight)), (int) (tHeight), 0)); - iconCache.put(icon, scaledIcon); - return scaledIcon; - } - public abstract String getWindowTitle(); protected final void addPage(WizardPage page) { diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java index daa67790..694056cc 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java @@ -1,14 +1,20 @@ package org.openslx.dozmod.util; import java.awt.Color; +import java.awt.Component; import java.awt.Font; import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.RenderingHints; import java.awt.font.FontRenderContext; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.InputStream; import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import javax.swing.Icon; import javax.swing.ImageIcon; import org.apache.commons.io.IOUtils; @@ -56,6 +62,11 @@ public class ResourceLoader { return new ImageIcon(); } } + + public static Icon getIcon(String path, String description, int maxHeight, Component context) { + ImageIcon icon = getIcon(path, description); + return getScaledIcon(icon, maxHeight, context); + } /** * Load the given resource as an ImageIcon. @@ -126,4 +137,31 @@ public class ResourceLoader { public static InputStream getStream(String path) { return ResourceLoader.class.getResourceAsStream(path); } + + private static final Map iconCache = new HashMap<>(); + + public static Icon getScaledIcon(Icon icon, int height, Component context) { + if (icon == null) + return null; + ImageIcon cached = iconCache.get(icon); + if (cached != null && cached.getIconHeight() == height) + return cached; + // Generate? + float iHeight = icon.getIconHeight(); + float tHeight = height; + if (iHeight <= tHeight) + return icon; // Small enough + // Scale down: + BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), + BufferedImage.TYPE_INT_ARGB); + Graphics2D g = image.createGraphics(); + g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + icon.paintIcon(context, g, 0, 0); + ImageIcon scaledIcon = new ImageIcon(image.getScaledInstance( + (int) (icon.getIconWidth() * (tHeight / iHeight)), (int) (tHeight), Image.SCALE_SMOOTH)); + iconCache.put(icon, scaledIcon); + return scaledIcon; + } + } \ No newline at end of file -- cgit v1.2.3-55-g7522