mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
reorganize cloud thumbnail code
This commit is contained in:
@@ -142,9 +142,9 @@ public class CanvasPanel extends JComponent implements Autoscroll {
|
||||
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||
.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, KeyEvent.CTRL_DOWN_MASK), "zoomOut");
|
||||
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, KeyEvent.META_DOWN_MASK), "zoomIn");
|
||||
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||
.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, KeyEvent.META_DOWN_MASK), "zoomOut");
|
||||
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, KeyEvent.META_DOWN_MASK), "zoomIn");
|
||||
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||
.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, KeyEvent.META_DOWN_MASK), "zoomOut");
|
||||
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||
.put(KeyStroke.getKeyStroke(KeyEvent.VK_0, KeyEvent.CTRL_DOWN_MASK), "resetZoom");
|
||||
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||
|
||||
@@ -62,7 +62,8 @@ public class CloudPlugIn implements IPlugIn {
|
||||
|
||||
private ISwingUI swingUI;
|
||||
private IPlugInPort plugInPort;
|
||||
private IPlugInPort thumbnailPresenter;
|
||||
private IPlugInPort thumbnailPresenter;
|
||||
private ThumbnailGenerator thumbnailGenerator;
|
||||
|
||||
private LibraryAction libraryAction;
|
||||
|
||||
@@ -81,7 +82,8 @@ public class CloudPlugIn implements IPlugIn {
|
||||
super();
|
||||
|
||||
this.swingUI = swingUI;
|
||||
this.thumbnailPresenter = new Presenter(new DummyView(), InMemoryConfigurationManager.getInstance());
|
||||
this.thumbnailPresenter = new Presenter(new DummyView(), InMemoryConfigurationManager.getInstance());
|
||||
this.thumbnailGenerator = new ThumbnailGenerator(thumbnailPresenter);
|
||||
|
||||
swingUI.injectMenuAction(getLibraryAction(), ONLINE_TITLE);
|
||||
swingUI.injectMenuAction(null, ONLINE_TITLE);
|
||||
@@ -441,7 +443,7 @@ public class CloudPlugIn implements IPlugIn {
|
||||
if (ButtonDialog.OK.equals(dialog.getSelectedButtonCaption())) {
|
||||
try {
|
||||
final File thumbnailFile = File.createTempFile("upload-thumbnail", ".png");
|
||||
if (ImageIO.write(dialog.getThumbnail(), "png", thumbnailFile)) {
|
||||
if (ImageIO.write(CloudPlugIn.this.thumbnailGenerator.getThumbnail(), "png", thumbnailFile)) {
|
||||
swingUI.executeBackgroundTask(new ITask<Void>() {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
|
||||
DIY Layout Creator (DIYLC).
|
||||
Copyright (c) 2009-2018 held jointly by the individual authors.
|
||||
|
||||
This file is part of DIYLC.
|
||||
|
||||
DIYLC is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
DIYLC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
package org.diylc.swing.plugins.cloud;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import org.diylc.common.DrawOption;
|
||||
import org.diylc.common.IPlugInPort;
|
||||
|
||||
/**
|
||||
* Generates thumbnail image with hard-coded dimensions.
|
||||
*
|
||||
* @author bancika
|
||||
*
|
||||
*/
|
||||
public class ThumbnailGenerator {
|
||||
|
||||
private IPlugInPort plugInPort;
|
||||
|
||||
public ThumbnailGenerator(IPlugInPort plugInPort) {
|
||||
this.plugInPort = plugInPort;
|
||||
}
|
||||
|
||||
public BufferedImage getThumbnail() {
|
||||
Dimension d = getThumbnailSize();
|
||||
BufferedImage thumbnailImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D cg = thumbnailImage.createGraphics();
|
||||
|
||||
paintThumbnail(cg, new Rectangle(thumbnailImage.getWidth(), thumbnailImage.getHeight()));
|
||||
return thumbnailImage;
|
||||
}
|
||||
|
||||
public Dimension getThumbnailSize() {
|
||||
Dimension d = this.plugInPort.getCanvasDimensions(false, false);
|
||||
if (d.height > d.width)
|
||||
return new Dimension(192 * d.width / d.height, 192);
|
||||
else
|
||||
return new Dimension(192, 192 * d.height / d.width);
|
||||
}
|
||||
|
||||
public void paintThumbnail(Graphics g, Rectangle rect) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
Dimension d = this.plugInPort.getCanvasDimensions(false, false);
|
||||
|
||||
g2d.setColor(Color.white);
|
||||
g2d.fill(rect);
|
||||
|
||||
double projectRatio = d.getWidth() / d.getHeight();
|
||||
double actualRatio = rect.getWidth() / rect.getHeight();
|
||||
double zoomRatio;
|
||||
if (projectRatio > actualRatio) {
|
||||
zoomRatio = rect.getWidth() / d.getWidth();
|
||||
} else {
|
||||
zoomRatio = rect.getHeight() / d.getHeight();
|
||||
}
|
||||
|
||||
// g2d.scale(zoomRatio, zoomRatio);
|
||||
this.plugInPort.draw(g2d, EnumSet.of(DrawOption.ANTIALIASING), null, zoomRatio, null);
|
||||
}
|
||||
}
|
||||
@@ -21,37 +21,32 @@
|
||||
*/
|
||||
package org.diylc.swing.plugins.cloud.view;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.diylc.common.DrawOption;
|
||||
import org.diylc.common.IPlugInPort;
|
||||
import org.diylc.common.PropertyWrapper;
|
||||
import org.diylc.swing.gui.components.HTMLTextArea;
|
||||
import org.diylc.swingframework.ButtonDialog;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.diylc.common.IPlugInPort;
|
||||
import org.diylc.common.PropertyWrapper;
|
||||
import org.diylc.swing.gui.components.HTMLTextArea;
|
||||
import org.diylc.swing.plugins.cloud.ThumbnailGenerator;
|
||||
import org.diylc.swingframework.ButtonDialog;
|
||||
import org.diylc.utils.KeywordExtractor;
|
||||
|
||||
public class UploadDialog extends ButtonDialog {
|
||||
@@ -79,11 +74,14 @@ public class UploadDialog extends ButtonDialog {
|
||||
private JTextField keywordsField;
|
||||
|
||||
private IPlugInPort plugInPort;
|
||||
private String[] categories;
|
||||
private String[] categories;
|
||||
|
||||
private ThumbnailGenerator thumbnailGenerator;
|
||||
|
||||
public UploadDialog(JFrame owner, IPlugInPort plugInPort, String[] categories, boolean isUpdate) {
|
||||
super(owner, isUpdate ? "Re-Upload A Project" : "Upload A Project", new String[] {OK, CANCEL});
|
||||
this.plugInPort = plugInPort;
|
||||
this.plugInPort = plugInPort;
|
||||
this.thumbnailGenerator = new ThumbnailGenerator(plugInPort);
|
||||
this.categories = categories;
|
||||
layoutGui();
|
||||
getButton(OK).setEnabled(false);
|
||||
@@ -205,24 +203,16 @@ public class UploadDialog extends ButtonDialog {
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
|
||||
paintThumbnail(g, getBounds());
|
||||
UploadDialog.this.thumbnailGenerator.paintThumbnail(g, getBounds());
|
||||
}
|
||||
}, gbc);
|
||||
|
||||
thumbnailPanel.setPreferredSize(getThumbnailSize());
|
||||
thumbnailPanel.setPreferredSize(this.thumbnailGenerator.getThumbnailSize());
|
||||
thumbnailPanel.setBorder(BorderFactory.createEtchedBorder());
|
||||
}
|
||||
return thumbnailPanel;
|
||||
}
|
||||
|
||||
private Dimension getThumbnailSize() {
|
||||
Dimension d = UploadDialog.this.plugInPort.getCanvasDimensions(false, false);
|
||||
if (d.height > d.width)
|
||||
return new Dimension(192 * d.width / d.height, 192);
|
||||
else
|
||||
return new Dimension(192, 192 * d.height / d.width);
|
||||
}
|
||||
|
||||
private JTextField getNameField() {
|
||||
if (nameField == null) {
|
||||
nameField = new JTextField();
|
||||
@@ -293,35 +283,6 @@ public class UploadDialog extends ButtonDialog {
|
||||
return getCategoryBox().getSelectedItem().toString();
|
||||
}
|
||||
|
||||
public BufferedImage getThumbnail() {
|
||||
Dimension d = getThumbnailSize();
|
||||
BufferedImage thumbnailImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D cg = thumbnailImage.createGraphics();
|
||||
|
||||
paintThumbnail(cg, new Rectangle(thumbnailImage.getWidth(), thumbnailImage.getHeight()));
|
||||
return thumbnailImage;
|
||||
}
|
||||
|
||||
private void paintThumbnail(Graphics g, Rectangle rect) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
Dimension d = UploadDialog.this.plugInPort.getCanvasDimensions(false, false);
|
||||
|
||||
g2d.setColor(Color.white);
|
||||
g2d.fill(rect);
|
||||
|
||||
double projectRatio = d.getWidth() / d.getHeight();
|
||||
double actualRatio = rect.getWidth() / rect.getHeight();
|
||||
double zoomRatio;
|
||||
if (projectRatio > actualRatio) {
|
||||
zoomRatio = rect.getWidth() / d.getWidth();
|
||||
} else {
|
||||
zoomRatio = rect.getHeight() / d.getHeight();
|
||||
}
|
||||
|
||||
// g2d.scale(zoomRatio, zoomRatio);
|
||||
UploadDialog.this.plugInPort.draw(g2d, EnumSet.of(DrawOption.ANTIALIASING), null, zoomRatio, null);
|
||||
}
|
||||
|
||||
public String getKeywords() {
|
||||
return getKeywordsField().getText();
|
||||
}
|
||||
|
||||
+8
-7
@@ -57,7 +57,6 @@ import org.diylc.common.IPlugInPort;
|
||||
import org.diylc.common.ITask;
|
||||
import org.diylc.common.PropertyWrapper;
|
||||
import org.diylc.core.IView;
|
||||
import org.diylc.swing.images.IconLoader;
|
||||
import org.diylc.plugins.cloud.model.CommentEntity;
|
||||
import org.diylc.plugins.cloud.model.ProjectEntity;
|
||||
import org.diylc.plugins.cloud.presenter.CloudPresenter;
|
||||
@@ -69,8 +68,9 @@ import org.diylc.swing.gui.DialogFactory;
|
||||
import org.diylc.swing.gui.DummyView;
|
||||
import org.diylc.swing.gui.components.HTMLTextArea;
|
||||
import org.diylc.swing.gui.editor.PropertyEditorDialog;
|
||||
import org.diylc.swing.images.IconLoader;
|
||||
import org.diylc.swing.plugins.cloud.ThumbnailGenerator;
|
||||
import org.diylc.swing.plugins.cloud.view.CommentDialog;
|
||||
import org.diylc.swing.plugins.cloud.view.UploadDialog;
|
||||
import org.diylc.swing.plugins.file.FileFilterEnum;
|
||||
import org.diylc.swingframework.ButtonDialog;
|
||||
import org.diylc.utils.Pair;
|
||||
@@ -400,8 +400,11 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
+ project.getName()
|
||||
+ "\" with a new file?\nThis opperation is irreversible. Once replaced, the old version of the project cannot be restored.",
|
||||
"Replace Project", IView.YES_NO_OPTION, IView.QUESTION_MESSAGE) == IView.YES_OPTION) {
|
||||
|
||||
final Presenter thumbnailPresenter =
|
||||
new Presenter(new DummyView(), InMemoryConfigurationManager.getInstance());
|
||||
final ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator(thumbnailPresenter);
|
||||
|
||||
final File file =
|
||||
DialogFactory.getInstance().showOpenDialog(FileFilterEnum.DIY.getFilter(), null,
|
||||
FileFilterEnum.DIY.getExtensions()[0], null, cloudUI.getOwnerFrame());
|
||||
@@ -413,15 +416,13 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
@Override
|
||||
public Pair<BufferedImage, ProjectEntity> doInBackground() throws Exception {
|
||||
LOG.debug("Uploading from " + file.getAbsolutePath());
|
||||
thumbnailPresenter.loadProjectFromFile(file.getAbsolutePath());
|
||||
final UploadDialog dialog = DialogFactory.getInstance().createUploadDialog(
|
||||
cloudUI.getOwnerFrame(), thumbnailPresenter, new String[0], true);
|
||||
thumbnailPresenter.loadProjectFromFile(file.getAbsolutePath());
|
||||
final File thumbnailFile = File.createTempFile("upload-thumbnail", ".png");
|
||||
if (ImageIO.write(dialog.getThumbnail(), "png", thumbnailFile)) {
|
||||
if (ImageIO.write(thumbnailGenerator.getThumbnail(), "png", thumbnailFile)) {
|
||||
CloudPresenter.Instance.replaceProjectFile(
|
||||
plugInPort.getCurrentVersionNumber().toString(), thumbnailFile, file,
|
||||
project.getId());
|
||||
return new Pair<BufferedImage, ProjectEntity>(dialog.getThumbnail(),
|
||||
return new Pair<BufferedImage, ProjectEntity>(thumbnailGenerator.getThumbnail(),
|
||||
CloudPresenter.Instance.fetchUserUploads(project.getId()).get(0));
|
||||
} else {
|
||||
cloudUI.showMessage(
|
||||
|
||||
Reference in New Issue
Block a user