Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hu.kazocsaba.imageviewer;
  
  import java.awt.Insets;
 import java.io.File;
The default popup menu for image viewers. The contents of the menu are unspecified and may change between library versions.

Author(s):
Kazó Csaba
 
 public class DefaultViewerPopup extends JPopupMenu {
 	private final ImageViewer viewer;
 	/*
 	 * These will only be accessed from the event dispatch thread so using a static instance to share
 	 * the current directory across components is fine.
 	 */
 	private static JFileChooser saveChooser;
 	private static JButton saveChooserHelpButton;
 	private static JLabel saveChooserHelpLabel;

Creates a popup menu for use with the specified viewer.

Parameters:
imageViewer the viewer this popup menu belongs to
 
 	public DefaultViewerPopup(ImageViewer imageViewer) {
 		=imageViewer;

Status bar toggle
 
 		
 		final JCheckBoxMenuItem toggleStatusBarItem = new JCheckBoxMenuItem("Status bar");
 		toggleStatusBarItem.setState(.isStatusBarVisible());
 
 			public void propertyChange(PropertyChangeEvent evt) {
 				toggleStatusBarItem.setState(.isStatusBarVisible());
 			}
 		});
 		toggleStatusBarItem.addActionListener(new ActionListener() {
 
 			public void actionPerformed(ActionEvent e) {
 			}
 		});

Zoom menu
 
 		
 		JMenu zoomMenu = new JMenu("Zoom");
 		final JRadioButtonMenuItem zoomOriginalSize = new JRadioButtonMenuItem("Original size".getResizeStrategy()==.);
 		zoomOriginalSize.addActionListener(new ActionListener() {
 
 			public void actionPerformed(ActionEvent e) {
 			}
 		});
 		final JRadioButtonMenuItem zoomShrinkToFit = new JRadioButtonMenuItem("Shrink to fit".getResizeStrategy()==.);
 		zoomShrinkToFit.addActionListener(new ActionListener() {
 
 			public void actionPerformed(ActionEvent e) {
 			}
 		});
 		final JRadioButtonMenuItem zoomResizeToFit = new JRadioButtonMenuItem("Resize to fit".getResizeStrategy()==.);
 		zoomResizeToFit.addActionListener(new ActionListener() {
 
 			public void actionPerformed(ActionEvent e) {
 			}
 		});
 		
 		class CustomZoomEntry {
 			double value;
			private CustomZoomEntry(String labeldouble value) {
				this. = label;
				this. = value;
					public void actionPerformed(ActionEvent e) {
					}
				});
			}
		}
		final CustomZoomEntry[] customZoomEntries={
			new CustomZoomEntry("25%", .25),
			new CustomZoomEntry("50%", .50),
			new CustomZoomEntry("75%", .75),
			new CustomZoomEntry("100%", 1),
			new CustomZoomEntry("150%", 1.5),
			new CustomZoomEntry("200%", 2),
			new CustomZoomEntry("300%", 3),
			new CustomZoomEntry("500%", 5),
			new CustomZoomEntry("1000%", 10),
			new CustomZoomEntry("2000%", 20),
			new CustomZoomEntry("5000%", 50)
		};
		final ButtonGroup group = new ButtonGroup();
		group.add(zoomOriginalSize);
		group.add(zoomShrinkToFit);
		group.add(zoomResizeToFit);
		zoomMenu.add(zoomOriginalSize);
		zoomMenu.add(zoomShrinkToFit);
		zoomMenu.add(zoomResizeToFit);
		zoomMenu.add(new JSeparator());
		for (CustomZoomEntry czecustomZoomEntries) {
			zoomMenu.add(cze.menuItem);
			group.add(cze.menuItem);
		}
			public void propertyChange(PropertyChangeEvent evt) {
				switch ((ResizeStrategy)evt.getNewValue()) {
					case :
						zoomOriginalSize.setSelected(true);
						break;
						zoomResizeToFit.setSelected(true);
						break;
						zoomShrinkToFit.setSelected(true);
						break;
						for (CustomZoomEntry czecustomZoomEntries) {
							if (cze.value==.getZoomFactor()) {
								cze.menuItem.setSelected(true);
								break;
							}
						}
						break;
					default:
						throw new AssertionError("Unknown resize strategy: "+evt.getNewValue());
				}
			}
		});
			public void propertyChange(PropertyChangeEvent evt) {
					for (CustomZoomEntry czecustomZoomEntries) {
						if (cze.value==.getZoomFactor()) {
							cze.menuItem.setSelected(true);
							break;
						}
					}
				}
			}
		});

Save command
		JMenuItem saveImageMenuItem=new JMenuItem("Save image...");
		saveImageMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (==null) {
					.setText("<html>If the file name ends<br>with '.png' or '.jpg',<br>then the appropriate<br>format is used.<br>Otherwise '.png' is<br>appended to the name.");
						public void actionPerformed(ActionEvent e) {
						}
					});
						public void mouseClicked(MouseEvent e) {
						}
					});
					.setDialogTitle("Save image...");
					.setFileFilter(new FileNameExtensionFilter("JPG and PNG images""jpg""png"));
				}
				// reset to show the help button with every new dialog
					if (image==null) {
						JOptionPane.showMessageDialog(.getComponent(), "No image""Error".);
else {
						try {
							if (name.endsWith(".jpg")) {
								ImageIO.write(image"jpg"f);
else if (name.endsWith(".png")) {
								ImageIO.write(image"png"f);
else {
								f=new File(f.getPath()+".png");
								ImageIO.write(image"png"f);
							}
catch (IOException ex) {
							JOptionPane.showMessageDialog(.getComponent(), "<html>Cannot write image to "+f.getAbsolutePath()+":<br>"+ex.getMessage(), "Error".);
						}
					}
				}
			}
		});

Pixelated zoom toggle
		final JCheckBoxMenuItem togglePixelatedZoomItem = new JCheckBoxMenuItem("Pixelated zoom");
		togglePixelatedZoomItem.setState(.isPixelatedZoom());
			public void propertyChange(PropertyChangeEvent evt) {
				togglePixelatedZoomItem.setState(.isPixelatedZoom());
			}
		});
		togglePixelatedZoomItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		add(toggleStatusBarItem);
		add(zoomMenu);
		add(togglePixelatedZoomItem);
		add(saveImageMenuItem);
	}
New to GrepCode? Check out our FAQ X