Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.sun.swingset3.demos.filechooser;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import com.sun.swingset3.demos.JGridPanel;
import com.sun.swingset3.demos.ResourceManager;
import com.sun.swingset3.DemoProperties;
/**
* JFileChooserDemo
*
* @author Pavel Porvatov
*/
@DemoProperties(
value = "JFileChooser Demo",
category = "Choosers",
description = "Demonstrates JFileChooser, a component which allows the user to open and save files.",
sourceFiles = {
"com/sun/swingset3/demos/filechooser/FileChooserDemo.java",
"com/sun/swingset3/demos/DemoUtilities.java",
"com/sun/swingset3/demos/filechooser/resources/FileChooserDemo.properties",
"com/sun/swingset3/demos/filechooser/resources/images/apply.png",
"com/sun/swingset3/demos/filechooser/resources/images/FileChooserDemo.gif",
"com/sun/swingset3/demos/filechooser/resources/images/fliphor.png",
"com/sun/swingset3/demos/filechooser/resources/images/flipvert.png",
"com/sun/swingset3/demos/filechooser/resources/images/rotateleft.png",
"com/sun/swingset3/demos/filechooser/resources/images/rotateright.png"
}
)
public class FileChooserDemo extends JPanel {
private enum State {
EMPTY,
IMAGE_LOADED,
IMAGE_CHANGED
}
private static final int MIN_FILTER_ID = 0;
private static final int MAX_FILTER_ID = 7;
private static final String[] FILTER_NAMES = {
"FileChooserDemo.filter.blur",
"FileChooserDemo.filter.edge",
"FileChooserDemo.filter.sharpen",
"FileChooserDemo.filter.darken",
"FileChooserDemo.filter.brighten",
"FileChooserDemo.filter.lesscontrast",
"FileChooserDemo.filter.morecontrast",
"FileChooserDemo.filter.gray"
};
private static final BufferedImageOp[] FILTER_OPERATIONS = {
new ConvolveOp(new Kernel(3, 3,
new float[]{.1111f, .1111f, .1111f, .1111f, .1111f, .1111f, .1111f, .1111f, .1111f}),
ConvolveOp.EDGE_NO_OP, null),
new ConvolveOp(new Kernel(3, 3,
new float[]{0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f, 0.0f}),
ConvolveOp.EDGE_NO_OP, null),
new ConvolveOp(new Kernel(3, 3,
new float[]{0.0f, -1.0f, 0.0f, -1.0f, 5.f, -1.0f, 0.0f, -1.0f, 0.0f}),
ConvolveOp.EDGE_NO_OP, null),
new RescaleOp(1, -5.0f, null),
new RescaleOp(1, 5.0f, null),
new RescaleOp(0.9f, 0, null),
new RescaleOp(1.1f, 0, null),
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null)
};
private final ResourceManager resourceManager = new ResourceManager(this.getClass());
private final JLabel lbImage = new JLabel(resourceManager.getString("FileChooserDemo.image.text"), JLabel.CENTER);
private final JScrollPane pnImage = new JScrollPane(lbImage);
private final JButton btnSelect = new JButton(resourceManager.getString("FileChooserDemo.select.text"));
private final JButton btnSelectWithPreview = new JButton(resourceManager.getString("FileChooserDemo.selectwithpreview.text"));
private final JComboBox cbFilters = new JComboBox();
private final JButton btnApplyFilter = createButton("apply.png", "FileChooserDemo.applyfilter.tooltip");
private final JButton btnRotateLeft = createButton("rotateleft.png", "FileChooserDemo.rotateleft.tooltip");
private final JButton btnRotateRight = createButton("rotateright.png", "FileChooserDemo.rotateright.tooltip");
private final JButton btnFlipHorizontal = createButton("fliphor.png", "FileChooserDemo.fliphorizontal.tooltip");
private final JButton btnFlipVertical = createButton("flipvert.png", "FileChooserDemo.flipvertical.tooltip");
private final JButton btnSave = new JButton(resourceManager.getString("FileChooserDemo.save.text"));
private final JButton btnCancel = new JButton(resourceManager.getString("FileChooserDemo.cancel.text"));
private final JFileChooser externalChooser = new JFileChooser();
private final JFileChooser embeddedChooser = new JFileChooser();
private final JGridPanel pnContent = new JGridPanel(1, 0, 0);
private State state;
private boolean fileChoosing;
private File file;
private BufferedImage image;
/**
* main method allows us to run as a standalone demo.
*/
public static void main(String[] args) {
JFrame frame = new JFrame(FileChooserDemo.class.getAnnotation(DemoProperties.class).value());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new FileChooserDemo());
frame.setPreferredSize(new Dimension(800, 600));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
/**
* FileChooserDemo Constructor
*/
public FileChooserDemo() {
setLayout(new BorderLayout());
initUI();
embeddedChooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (JFileChooser.APPROVE_SELECTION.equals(e.getActionCommand())) {
loadFile(embeddedChooser.getSelectedFile());
}
if (JFileChooser.CANCEL_SELECTION.equals(e.getActionCommand())) {
setState(state, false);
}
}
});
btnSelect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (fileChoosing) {
loadFile(embeddedChooser.getSelectedFile());
} else {
setState(state, true);
}
}
});
btnSelectWithPreview.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (externalChooser.showOpenDialog(FileChooserDemo.this) == JFileChooser.APPROVE_OPTION) {
loadFile(externalChooser.getSelectedFile());
}
}
});
btnApplyFilter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doFilter(FILTER_OPERATIONS[((FilterItem) cbFilters.getSelectedItem()).getId()]);
}
});
btnRotateLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doAffineTransform(image.getHeight(), image.getWidth(),
new AffineTransform(0, -1, 1, 0, 0, image.getWidth()));
}
});
btnRotateRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doAffineTransform(image.getHeight(), image.getWidth(),
new AffineTransform(0, 1, -1, 0, image.getHeight(), 0));
}
});
btnFlipHorizontal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doAffineTransform(image.getWidth(), image.getHeight(),
new AffineTransform(-1, 0, 0, 1, image.getWidth(), 0));
}
});
btnFlipVertical.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doAffineTransform(image.getWidth(), image.getHeight(),
new AffineTransform(1, 0, 0, -1, 0, image.getHeight()));
}
});
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(FileChooserDemo.this,
resourceManager.getString("FileChooserDemo.savequiestion.message"),
resourceManager.getString("FileChooserDemo.savequiestion.title"),
JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
return;
}
String fileName = file.getName();
int i = fileName.lastIndexOf('.');
try {
ImageIO.write(image, fileName.substring(i + 1), file);
setState(State.IMAGE_LOADED, false);
} catch (IOException e1) {
JOptionPane.showMessageDialog(FileChooserDemo.this,
MessageFormat.format(resourceManager.getString("FileChooserDemo.errorsavefile.message"), e1),
resourceManager.getString("FileChooserDemo.errorsavefile.title"),
JOptionPane.ERROR_MESSAGE);
}
}
});
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadFile(file);
}
});
}
private void initUI() {
externalChooser.addChoosableFileFilter(new FileNameExtensionFilter("JPEG images", "jpg"));
externalChooser.addChoosableFileFilter(new FileNameExtensionFilter("All supported images",
ImageIO.getWriterFormatNames()));
final FilePreview filePreview = new FilePreview();
externalChooser.setAccessory(filePreview);
externalChooser.addPropertyChangeListener(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY,
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
filePreview.loadFileInfo(externalChooser.getSelectedFile());
}
});
embeddedChooser.setControlButtonsAreShown(false);
embeddedChooser.addChoosableFileFilter(new FileNameExtensionFilter("JPEG images", "jpg"));
FileNameExtensionFilter filter = new FileNameExtensionFilter("All supported images",
ImageIO.getWriterFormatNames());
embeddedChooser.addChoosableFileFilter(filter);
embeddedChooser.setFileFilter(filter);
for (int i = MIN_FILTER_ID; i <= MAX_FILTER_ID; i++) {
cbFilters.addItem(new FilterItem(i, resourceManager.getString(FILTER_NAMES[i])));
}
JGridPanel pnFilter = new JGridPanel(2, 0);
pnFilter.cell(cbFilters).
cell(btnApplyFilter);
JGridPanel pnRotateButtons = new JGridPanel(4, 3);
pnRotateButtons.cell(btnRotateLeft).
cell(btnRotateRight).
cell(btnFlipHorizontal).
cell(btnFlipVertical);
JGridPanel pnBottom = new JGridPanel(4, 1);
pnBottom.setHGap(JGridPanel.DEFAULT_GAP * 4);
pnBottom.cell(btnSelect, JGridPanel.Layout.FILL).
cell().
cell(pnFilter).
cell(btnSave, JGridPanel.Layout.FILL).
cell(btnSelectWithPreview, JGridPanel.Layout.FILL).
cell().
cell(pnRotateButtons).
cell(btnCancel, JGridPanel.Layout.FILL);
pnContent.cell(pnImage);
pnContent.cell(pnBottom, new Insets(10, 10, 10, 10));
add(pnContent);
setState(State.EMPTY, false);
}
private JButton createButton(String image, String toolTip) {
JButton res = new JButton(resourceManager.createImageIcon(image, null));
res.setPreferredSize(new Dimension(26, 26));
res.setMinimumSize(new Dimension(26, 26));
res.setToolTipText(resourceManager.getString(toolTip));
return res;
}
private void doAffineTransform(int width, int height, AffineTransform transform) {
BufferedImage newImage = new BufferedImage(image.getColorModel(),
image.getRaster().createCompatibleWritableRaster(width, height),
image.isAlphaPremultiplied(), new Hashtable