Browse Source

background image anfang

Kevin Trometer 7 years ago
parent
commit
3d08e3b8af

+ 92 - 0
src/org/eclipse/wb/swing/FocusTraversalOnArray.java

@@ -0,0 +1,92 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Google, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *    Google, Inc. - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.wb.swing;
+
+import java.awt.Component;
+import java.awt.Container;
+import java.awt.FocusTraversalPolicy;
+
+/**
+ * Cyclic focus traversal policy based on array of components.
+ * <p>
+ * This class may be freely distributed as part of any application or plugin.
+ * 
+ * @author scheglov_ke
+ */
+public class FocusTraversalOnArray extends FocusTraversalPolicy {
+	private final Component m_Components[];
+	////////////////////////////////////////////////////////////////////////////
+	//
+	// Constructor
+	//
+	////////////////////////////////////////////////////////////////////////////
+	public FocusTraversalOnArray(Component components[]) {
+		m_Components = components;
+	}
+	////////////////////////////////////////////////////////////////////////////
+	//
+	// Utilities
+	//
+	////////////////////////////////////////////////////////////////////////////
+	private int indexCycle(int index, int delta) {
+		int size = m_Components.length;
+		int next = (index + delta + size) % size;
+		return next;
+	}
+	private Component cycle(Component currentComponent, int delta) {
+		int index = -1;
+		loop : for (int i = 0; i < m_Components.length; i++) {
+			Component component = m_Components[i];
+			for (Component c = currentComponent; c != null; c = c.getParent()) {
+				if (component == c) {
+					index = i;
+					break loop;
+				}
+			}
+		}
+		// try to find enabled component in "delta" direction
+		int initialIndex = index;
+		while (true) {
+			int newIndex = indexCycle(index, delta);
+			if (newIndex == initialIndex) {
+				break;
+			}
+			index = newIndex;
+			//
+			Component component = m_Components[newIndex];
+			if (component.isEnabled() && component.isVisible() && component.isFocusable()) {
+				return component;
+			}
+		}
+		// not found
+		return currentComponent;
+	}
+	////////////////////////////////////////////////////////////////////////////
+	//
+	// FocusTraversalPolicy
+	//
+	////////////////////////////////////////////////////////////////////////////
+	public Component getComponentAfter(Container container, Component component) {
+		return cycle(component, 1);
+	}
+	public Component getComponentBefore(Container container, Component component) {
+		return cycle(component, -1);
+	}
+	public Component getFirstComponent(Container container) {
+		return m_Components[0];
+	}
+	public Component getLastComponent(Container container) {
+		return m_Components[m_Components.length - 1];
+	}
+	public Component getDefaultComponent(Container container) {
+		return getFirstComponent(container);
+	}
+}

+ 93 - 0
src/ui/view/BackgroundPopUp.java

@@ -0,0 +1,93 @@
+package ui.view;
+
+import javax.swing.JDialog;
+import javax.swing.JFileChooser;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+import javax.swing.filechooser.FileNameExtensionFilter;
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import org.eclipse.wb.swing.FocusTraversalOnArray;
+
+import javafx.scene.image.Image;
+import javafx.stage.FileChooser;
+
+import java.awt.Component;
+import java.awt.Window;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.File;
+import java.awt.BorderLayout;
+import javax.swing.JLabel;
+import javax.swing.JRadioButton;
+import javax.swing.BoxLayout;
+import javax.swing.SwingConstants;
+import java.awt.FlowLayout;
+import java.awt.Choice;
+
+/**
+ * Popup for setting the Background Image for the current View.
+ **/
+public class BackgroundPopUp extends JDialog {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+	private final JTextField textPath = new JTextField("");
+	private final JButton btnBrowse = new JButton("Browse");
+	private JPanel panelImageRadio = new JPanel();
+
+	private String path = "";
+	private final JPanel panelBrowse = new JPanel();
+	private final JPanel panelOK = new JPanel();
+	private final JButton btnOK = new JButton("OK");
+	private final JLabel lblImage = new JLabel();
+
+	private double imgScale = 1;
+
+	public BackgroundPopUp() {
+		super((java.awt.Frame) null, true);
+		this.setTitle("Set View Background");
+		setBounds(100, 100, 400, 250);
+
+		getContentPane().add(panelImageRadio, BorderLayout.CENTER);
+		panelImageRadio.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
+		lblImage.setHorizontalAlignment(SwingConstants.RIGHT);
+
+		panelImageRadio.add(lblImage);
+		getContentPane().add(panelBrowse, BorderLayout.NORTH);
+		panelBrowse.add(btnBrowse);
+
+		// Browse Row Functions
+		btnBrowse.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				JFileChooser fileChooser = new JFileChooser();
+				FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
+				fileChooser.setFileFilter(filter);
+				if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
+					textPath.setText(fileChooser.getSelectedFile().getPath());
+					ImageIcon icon = new ImageIcon(textPath.getText());
+					imgScale = Math.max(icon.getIconWidth(), icon.getIconHeight()) / 200;
+					lblImage.setIcon(new ImageIcon(new ImageIcon(textPath.getText()).getImage().getScaledInstance(
+							(int) (icon.getIconWidth() / imgScale), (int) (icon.getIconHeight() / imgScale),
+							java.awt.Image.SCALE_SMOOTH)));
+				}
+			}
+		});
+
+		textPath.setEditable(false);
+
+		panelBrowse.add(textPath);
+		textPath.setColumns(20);
+		panelImageRadio.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[] { btnBrowse, textPath }));
+
+		getContentPane().add(panelOK, BorderLayout.SOUTH);
+
+		panelOK.add(btnOK);
+
+	}
+
+}

+ 15 - 0
src/ui/view/GUI.java

@@ -105,6 +105,7 @@ public class GUI<E> implements CategoryListener {
 	private final JMenuItem mntmSave = new JMenuItem("Save");
 	private final JMenuItem aboutUs = new JMenuItem("About Us");
 	private final JMenuItem canvasSize = new JMenuItem("View Size");
+	private final JMenuItem background = new JMenuItem("Background Image");
 	private final JSplitPane splitPane = new JSplitPane();
 	private final JSplitPane splitPane1 = new JSplitPane();
 	private final JSplitPane splitPaneCanvasConsole = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
@@ -669,6 +670,20 @@ public class GUI<E> implements CategoryListener {
 				canvas.repaint();
 			}
 		});
+		
+		mnNewMenuView.add(canvasSize);
+		
+		mnNewMenuView.add(background);
+		background.addActionListener(new ActionListener() {
+			
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				BackgroundPopUp backgroundDialog = new BackgroundPopUp();
+				backgroundDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+				backgroundDialog.setVisible(true);
+			}
+		});
+		
 		splitPane3.setRightComponent(sizeSlider);
 
 		splitPane3.setLeftComponent(lblImageSize);