001package jmri.util; 002 003import java.awt.GridBagConstraints; 004import java.awt.GridBagLayout; 005import java.awt.Insets; 006import javax.swing.BorderFactory; 007import javax.swing.JLabel; 008import javax.swing.JPanel; 009import javax.swing.JSpinner; 010import javax.swing.SpinnerNumberModel; 011import javax.swing.border.EtchedBorder; 012import javax.swing.border.TitledBorder; 013import org.slf4j.Logger; 014import org.slf4j.LoggerFactory; 015 016/** 017 * Provides a Swing component to show and/or edit a PhysicalLocation 018 * <hr> 019 * This file is part of JMRI. 020 * <p> 021 * JMRI is free software; you can redistribute it and/or modify it under 022 * the terms of version 2 of the GNU General Public License as published 023 * by the Free Software Foundation. See the "COPYING" file for a copy 024 * of this license. 025 * <p> 026 * JMRI is distributed in the hope that it will be useful, but WITHOUT 027 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 028 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 029 * for more details. 030 * 031 * @author Mark Underwood Copyright (C) 2011 032 */ 033public class PhysicalLocationPanel extends JPanel { 034 035 TitledBorder tb; 036 JSpinner xs, ys, zs; 037 SpinnerNumberModel spinnerModel; 038 039 static final Float min_spin = -1000.0f; 040 static final Float max_spin = 1000.0f; 041 static final Float spin_value = 0.0f; 042 static final Float spin_inc = 0.1f; 043 044 /** 045 * Constructor 046 */ 047 public PhysicalLocationPanel() { 048 super(); 049 initComponents(""); 050 } 051 052 /** 053 * Constructor 054 * 055 * @param title (String) : Window title 056 */ 057 public PhysicalLocationPanel(String title) { 058 super(); 059 initComponents(title); 060 } 061 062 private GridBagConstraints setConstraints(int x, int y, boolean fill) { 063 GridBagConstraints gbc1 = new GridBagConstraints(); 064 gbc1.insets = new Insets(2, 2, 2, 2); 065 gbc1.gridx = x; 066 //gbc1.gridx = GridBagConstraints.RELATIVE; 067 gbc1.gridy = y; 068 gbc1.weightx = 100.0; 069 gbc1.weighty = 100.0; 070 gbc1.gridwidth = 1; 071 gbc1.anchor = GridBagConstraints.LINE_START; 072 return (gbc1); 073 } 074 075 protected void initComponents(String title) { 076 077 tb = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), title); 078 tb.setTitlePosition(TitledBorder.DEFAULT_POSITION); 079 this.setBorder(tb); 080 081 this.setLayout(new GridBagLayout()); 082 083 xs = new JSpinner(new SpinnerNumberModel(spin_value, min_spin, max_spin, spin_inc)); 084 ys = new JSpinner(new SpinnerNumberModel(spin_value, min_spin, max_spin, spin_inc)); 085 zs = new JSpinner(new SpinnerNumberModel(spin_value, min_spin, max_spin, spin_inc)); 086 087 JLabel xl = new JLabel("X"); 088 JLabel yl = new JLabel("Y"); 089 JLabel zl = new JLabel("Z"); 090 091 this.add(xl, setConstraints(0, 0, false)); 092 this.add(xs, setConstraints(GridBagConstraints.RELATIVE, 0, true)); 093 this.add(yl, setConstraints(GridBagConstraints.RELATIVE, 0, false)); 094 this.add(ys, setConstraints(GridBagConstraints.RELATIVE, 0, true)); 095 this.add(zl, setConstraints(GridBagConstraints.RELATIVE, 0, false)); 096 this.add(zs, setConstraints(GridBagConstraints.RELATIVE, 0, true)); 097 098 this.setVisible(true); 099 log.debug("initComponents() complete"); 100 } 101 102 /** 103 * Set the window tile 104 * 105 * @param t : new title 106 */ 107 public void setTitle(String t) { 108 tb.setTitle(t); 109 } 110 111 /** 112 * Retrieve the window title 113 * 114 * @return (String) title 115 */ 116 public String getTitle() { 117 return (tb.getTitle()); 118 } 119 120 /** 121 * Set the value of the pane 122 * 123 * @param p (PhysicalLocation) : value to set 124 */ 125 public void setValue(PhysicalLocation p) { 126 xs.setValue(p.getX()); 127 ys.setValue(p.getY()); 128 zs.setValue(p.getZ()); 129 } 130 131 /** 132 * Set the value of the pane 133 * 134 * @param s (String) : value to set 135 */ 136 public void setValue(String s) { 137 PhysicalLocation p = PhysicalLocation.parse(s); 138 if (p != null) { 139 this.setValue(p); 140 } 141 } 142 143 /** 144 * Get the value of the pane 145 * 146 * @return PhysicalLocation : Current value of pane 147 */ 148 public PhysicalLocation getValue() { 149 Float x = (Float) ((SpinnerNumberModel) xs.getModel()).getNumber(); 150 Float y = (Float) ((SpinnerNumberModel) ys.getModel()).getNumber(); 151 Float z = (Float) ((SpinnerNumberModel) zs.getModel()).getNumber(); 152 return (new PhysicalLocation(x, y, z)); 153 154 } 155 156 private static final Logger log = LoggerFactory.getLogger(PhysicalLocationPanel.class); 157 158}