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:
@@ -143,7 +143,7 @@ public class CanvasPanel extends JComponent implements Autoscroll {
|
|||||||
.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, KeyEvent.CTRL_DOWN_MASK), "zoomOut");
|
.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, KeyEvent.CTRL_DOWN_MASK), "zoomOut");
|
||||||
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||||
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, KeyEvent.META_DOWN_MASK), "zoomIn");
|
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, KeyEvent.META_DOWN_MASK), "zoomIn");
|
||||||
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||||
.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, KeyEvent.META_DOWN_MASK), "zoomOut");
|
.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, KeyEvent.META_DOWN_MASK), "zoomOut");
|
||||||
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||||
.put(KeyStroke.getKeyStroke(KeyEvent.VK_0, KeyEvent.CTRL_DOWN_MASK), "resetZoom");
|
.put(KeyStroke.getKeyStroke(KeyEvent.VK_0, KeyEvent.CTRL_DOWN_MASK), "resetZoom");
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ public class CloudPlugIn implements IPlugIn {
|
|||||||
private ISwingUI swingUI;
|
private ISwingUI swingUI;
|
||||||
private IPlugInPort plugInPort;
|
private IPlugInPort plugInPort;
|
||||||
private IPlugInPort thumbnailPresenter;
|
private IPlugInPort thumbnailPresenter;
|
||||||
|
private ThumbnailGenerator thumbnailGenerator;
|
||||||
|
|
||||||
private LibraryAction libraryAction;
|
private LibraryAction libraryAction;
|
||||||
|
|
||||||
@@ -82,6 +83,7 @@ public class CloudPlugIn implements IPlugIn {
|
|||||||
|
|
||||||
this.swingUI = swingUI;
|
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(getLibraryAction(), ONLINE_TITLE);
|
||||||
swingUI.injectMenuAction(null, ONLINE_TITLE);
|
swingUI.injectMenuAction(null, ONLINE_TITLE);
|
||||||
@@ -441,7 +443,7 @@ public class CloudPlugIn implements IPlugIn {
|
|||||||
if (ButtonDialog.OK.equals(dialog.getSelectedButtonCaption())) {
|
if (ButtonDialog.OK.equals(dialog.getSelectedButtonCaption())) {
|
||||||
try {
|
try {
|
||||||
final File thumbnailFile = File.createTempFile("upload-thumbnail", ".png");
|
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>() {
|
swingUI.executeBackgroundTask(new ITask<Void>() {
|
||||||
|
|
||||||
@Override
|
@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,16 +21,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.diylc.swing.plugins.cloud.view;
|
package org.diylc.swing.plugins.cloud.view;
|
||||||
|
|
||||||
import java.awt.Color;
|
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
|
||||||
import java.awt.GridBagConstraints;
|
import java.awt.GridBagConstraints;
|
||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.Rectangle;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.util.EnumSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.swing.BorderFactory;
|
import javax.swing.BorderFactory;
|
||||||
@@ -47,10 +42,10 @@ import javax.swing.UIManager;
|
|||||||
import javax.swing.event.ChangeEvent;
|
import javax.swing.event.ChangeEvent;
|
||||||
import javax.swing.event.ChangeListener;
|
import javax.swing.event.ChangeListener;
|
||||||
|
|
||||||
import org.diylc.common.DrawOption;
|
|
||||||
import org.diylc.common.IPlugInPort;
|
import org.diylc.common.IPlugInPort;
|
||||||
import org.diylc.common.PropertyWrapper;
|
import org.diylc.common.PropertyWrapper;
|
||||||
import org.diylc.swing.gui.components.HTMLTextArea;
|
import org.diylc.swing.gui.components.HTMLTextArea;
|
||||||
|
import org.diylc.swing.plugins.cloud.ThumbnailGenerator;
|
||||||
import org.diylc.swingframework.ButtonDialog;
|
import org.diylc.swingframework.ButtonDialog;
|
||||||
import org.diylc.utils.KeywordExtractor;
|
import org.diylc.utils.KeywordExtractor;
|
||||||
|
|
||||||
@@ -81,9 +76,12 @@ public class UploadDialog extends ButtonDialog {
|
|||||||
private IPlugInPort plugInPort;
|
private IPlugInPort plugInPort;
|
||||||
private String[] categories;
|
private String[] categories;
|
||||||
|
|
||||||
|
private ThumbnailGenerator thumbnailGenerator;
|
||||||
|
|
||||||
public UploadDialog(JFrame owner, IPlugInPort plugInPort, String[] categories, boolean isUpdate) {
|
public UploadDialog(JFrame owner, IPlugInPort plugInPort, String[] categories, boolean isUpdate) {
|
||||||
super(owner, isUpdate ? "Re-Upload A Project" : "Upload A Project", new String[] {OK, CANCEL});
|
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;
|
this.categories = categories;
|
||||||
layoutGui();
|
layoutGui();
|
||||||
getButton(OK).setEnabled(false);
|
getButton(OK).setEnabled(false);
|
||||||
@@ -205,24 +203,16 @@ public class UploadDialog extends ButtonDialog {
|
|||||||
public void paint(Graphics g) {
|
public void paint(Graphics g) {
|
||||||
super.paint(g);
|
super.paint(g);
|
||||||
|
|
||||||
paintThumbnail(g, getBounds());
|
UploadDialog.this.thumbnailGenerator.paintThumbnail(g, getBounds());
|
||||||
}
|
}
|
||||||
}, gbc);
|
}, gbc);
|
||||||
|
|
||||||
thumbnailPanel.setPreferredSize(getThumbnailSize());
|
thumbnailPanel.setPreferredSize(this.thumbnailGenerator.getThumbnailSize());
|
||||||
thumbnailPanel.setBorder(BorderFactory.createEtchedBorder());
|
thumbnailPanel.setBorder(BorderFactory.createEtchedBorder());
|
||||||
}
|
}
|
||||||
return thumbnailPanel;
|
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() {
|
private JTextField getNameField() {
|
||||||
if (nameField == null) {
|
if (nameField == null) {
|
||||||
nameField = new JTextField();
|
nameField = new JTextField();
|
||||||
@@ -293,35 +283,6 @@ public class UploadDialog extends ButtonDialog {
|
|||||||
return getCategoryBox().getSelectedItem().toString();
|
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() {
|
public String getKeywords() {
|
||||||
return getKeywordsField().getText();
|
return getKeywordsField().getText();
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-6
@@ -57,7 +57,6 @@ import org.diylc.common.IPlugInPort;
|
|||||||
import org.diylc.common.ITask;
|
import org.diylc.common.ITask;
|
||||||
import org.diylc.common.PropertyWrapper;
|
import org.diylc.common.PropertyWrapper;
|
||||||
import org.diylc.core.IView;
|
import org.diylc.core.IView;
|
||||||
import org.diylc.swing.images.IconLoader;
|
|
||||||
import org.diylc.plugins.cloud.model.CommentEntity;
|
import org.diylc.plugins.cloud.model.CommentEntity;
|
||||||
import org.diylc.plugins.cloud.model.ProjectEntity;
|
import org.diylc.plugins.cloud.model.ProjectEntity;
|
||||||
import org.diylc.plugins.cloud.presenter.CloudPresenter;
|
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.DummyView;
|
||||||
import org.diylc.swing.gui.components.HTMLTextArea;
|
import org.diylc.swing.gui.components.HTMLTextArea;
|
||||||
import org.diylc.swing.gui.editor.PropertyEditorDialog;
|
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.CommentDialog;
|
||||||
import org.diylc.swing.plugins.cloud.view.UploadDialog;
|
|
||||||
import org.diylc.swing.plugins.file.FileFilterEnum;
|
import org.diylc.swing.plugins.file.FileFilterEnum;
|
||||||
import org.diylc.swingframework.ButtonDialog;
|
import org.diylc.swingframework.ButtonDialog;
|
||||||
import org.diylc.utils.Pair;
|
import org.diylc.utils.Pair;
|
||||||
@@ -400,8 +400,11 @@ public class ResultsScrollPanel extends JScrollPane {
|
|||||||
+ project.getName()
|
+ project.getName()
|
||||||
+ "\" with a new file?\nThis opperation is irreversible. Once replaced, the old version of the project cannot be restored.",
|
+ "\" 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) {
|
"Replace Project", IView.YES_NO_OPTION, IView.QUESTION_MESSAGE) == IView.YES_OPTION) {
|
||||||
|
|
||||||
final Presenter thumbnailPresenter =
|
final Presenter thumbnailPresenter =
|
||||||
new Presenter(new DummyView(), InMemoryConfigurationManager.getInstance());
|
new Presenter(new DummyView(), InMemoryConfigurationManager.getInstance());
|
||||||
|
final ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator(thumbnailPresenter);
|
||||||
|
|
||||||
final File file =
|
final File file =
|
||||||
DialogFactory.getInstance().showOpenDialog(FileFilterEnum.DIY.getFilter(), null,
|
DialogFactory.getInstance().showOpenDialog(FileFilterEnum.DIY.getFilter(), null,
|
||||||
FileFilterEnum.DIY.getExtensions()[0], null, cloudUI.getOwnerFrame());
|
FileFilterEnum.DIY.getExtensions()[0], null, cloudUI.getOwnerFrame());
|
||||||
@@ -414,14 +417,12 @@ public class ResultsScrollPanel extends JScrollPane {
|
|||||||
public Pair<BufferedImage, ProjectEntity> doInBackground() throws Exception {
|
public Pair<BufferedImage, ProjectEntity> doInBackground() throws Exception {
|
||||||
LOG.debug("Uploading from " + file.getAbsolutePath());
|
LOG.debug("Uploading from " + file.getAbsolutePath());
|
||||||
thumbnailPresenter.loadProjectFromFile(file.getAbsolutePath());
|
thumbnailPresenter.loadProjectFromFile(file.getAbsolutePath());
|
||||||
final UploadDialog dialog = DialogFactory.getInstance().createUploadDialog(
|
|
||||||
cloudUI.getOwnerFrame(), thumbnailPresenter, new String[0], true);
|
|
||||||
final File thumbnailFile = File.createTempFile("upload-thumbnail", ".png");
|
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(
|
CloudPresenter.Instance.replaceProjectFile(
|
||||||
plugInPort.getCurrentVersionNumber().toString(), thumbnailFile, file,
|
plugInPort.getCurrentVersionNumber().toString(), thumbnailFile, file,
|
||||||
project.getId());
|
project.getId());
|
||||||
return new Pair<BufferedImage, ProjectEntity>(dialog.getThumbnail(),
|
return new Pair<BufferedImage, ProjectEntity>(thumbnailGenerator.getThumbnail(),
|
||||||
CloudPresenter.Instance.fetchUserUploads(project.getId()).get(0));
|
CloudPresenter.Instance.fetchUserUploads(project.getId()).get(0));
|
||||||
} else {
|
} else {
|
||||||
cloudUI.showMessage(
|
cloudUI.showMessage(
|
||||||
|
|||||||
Reference in New Issue
Block a user