package net.sf.cannagrower.gui;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.util.List;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.io.IOException;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JList;
import javax.swing.JSplitPane;
import net.sf.cannagrower.data.Culture;
import net.sf.cannagrower.CannaGrower;
import net.sf.cannagrower.CannaGrowerProperties;
import net.sf.cannagrower.i18n.Messages;
import net.sf.orexio.common.ui.Spotlight;
import javax.swing.JCheckBox;
public class FrameTutorial extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JSplitPane jSplitPane = null;
private JScrollPane jScrollPaneIndex = null;
private JScrollPane jScrollPaneText = null;
private JTextPane jTextPane = null;
private JList jListIndex = null;
private JCheckBox jCheckBoxShowAtStartup = null;
/**
* This is the default constructor
*/
public FrameTutorial() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
setIconImage(Toolkit.getDefaultToolkit().getImage(
getClass().getResource(
"/net/sf/cannagrower/images/help_32.png")));
this.setSize(680, 440);
this.setContentPane(getJContentPane());
this.setTitle(Messages.getMessage(Messages.cultureDemo));
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
close();
}
});
if(jListIndex.getModel().getSize()>0){jListIndex.setSelectedIndex(0);}
}
private void pageShow(){
DemoPage page;
List<Spotlight> spotlights;
FrameCulture cultureViewer;
cultureViewer=CannaGrower.cannagrower.getCultureActive();
if (jListIndex.getSelectedIndex() >= 0
&& jListIndex.getSelectedIndex() < jListIndex
.getModel().getSize()) {
page=(DemoPage)jListIndex.getSelectedValue();
// Show text
getJTextPane().setText(page.getText());
// Show spotlight
if(CannaGrower.cannagrower==null){return;}
CannaGrower.cannagrower.getJPanelSpotlight().clearSpotlights();
spotlights=page.getSpotlights(cultureViewer);
if(spotlights!=null){
for(Spotlight spotlight:spotlights){
if(cultureViewer!=null){if(cultureViewer.isVisible()){
CannaGrower.cannagrower.getJPanelSpotlight().add(spotlight);
}}
}
}
if(cultureViewer!=null){cultureViewer.moveToFront();}
}
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
jContentPane.add(getJCheckBoxShowAtStartup(), BorderLayout.SOUTH);
}
return jContentPane;
}
/**
* This method initializes jSplitPane
*
* @return javax.swing.JSplitPane
*/
private JSplitPane getJSplitPane() {
if (jSplitPane == null) {
jSplitPane = new JSplitPane();
jSplitPane.setDividerLocation(130);
jSplitPane.setLeftComponent(getJScrollPaneIndex());
jSplitPane.setRightComponent(getJScrollPaneText());
}
return jSplitPane;
}
/**
* This method initializes jScrollPaneIndex
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPaneIndex() {
if (jScrollPaneIndex == null) {
jScrollPaneIndex = new JScrollPane();
jScrollPaneIndex.setViewportView(getJListIndex());
}
return jScrollPaneIndex;
}
/**
* This method initializes jScrollPaneText
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPaneText() {
if (jScrollPaneText == null) {
jScrollPaneText = new JScrollPane();
jScrollPaneText.setViewportView(getJTextPane());
}
return jScrollPaneText;
}
/**
* This method initializes jTextPane
*
* @return javax.swing.JTextPane
*/
private JTextPane getJTextPane() {
if (jTextPane == null) {
jTextPane = new JTextPane();
jTextPane.setContentType("text/html");
}
return jTextPane;
}
/**
* This method initializes jListIndex
*
* @return javax.swing.JList
*/
private JList getJListIndex() {
if (jListIndex == null) {
Vector<DemoPage> pages=new Vector<DemoPage>();
pages.add(new Page1());
pages.add(new Page2());
pages.add(new Page3());
pages.add(new Page3_1());
pages.add(new Page4());
pages.add(new Page4_1());
jListIndex = new JList(pages);
jListIndex
.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent e) {
pageShow();
}
});
}
return jListIndex;
}
/**
* This method initializes jCheckBoxShowAtStartup
*
* @return javax.swing.JCheckBox
*/
private JCheckBox getJCheckBoxShowAtStartup() {
if (jCheckBoxShowAtStartup == null) {
jCheckBoxShowAtStartup = new JCheckBox();
jCheckBoxShowAtStartup.setText(Messages.getMessage(Messages.settingsShowThis));
jCheckBoxShowAtStartup.setSelected(CannaGrowerProperties.getProperties().getShowDemo());
}
return jCheckBoxShowAtStartup;
}
public void open(){
// Adjusting placement
Double y=(CannaGrower.cannagrower.getLocation().y+(CannaGrower.cannagrower.getSize().getHeight()-this.getSize().getHeight())/2);
Double x=CannaGrower.cannagrower.getLocation().x+CannaGrower.cannagrower.getSize().getWidth();
if(x+this.getSize().getWidth()>Toolkit.getDefaultToolkit().getScreenSize().width){
x=Toolkit.getDefaultToolkit().getScreenSize().width-this.getSize().getWidth();
}
this.setLocation(x.intValue(), y.intValue());
// Move on top
this.toFront();
this.setAlwaysOnTop(true);
// Show culture
openCulture();
}
private void openCulture(){
if(CannaGrower.cannagrower.getCultureActive()==null){
// Opening demo culture
Culture culture;
try{
culture=Culture.create(Culture.typeDemo);
}catch(IOException e){
Messages.showException(e);
return;
}catch(ClassNotFoundException e){
Messages.showException(e);
return;
}
CannaGrower.cannagrower.open(culture,null);
}
}
public void close(){
CannaGrower.cannagrower.getJPanelSpotlight().clearSpotlights();
net.sf.cannagrower.CannaGrowerProperties.getProperties().setShowDemo(jCheckBoxShowAtStartup.isSelected());
dispose();
}
} // @jve:decl-index=0:visual-constraint="10,10"
class DemoPage{
protected String name;
protected String text;
DemoPage(){
name=Messages.getMessage(Messages.cultureDemo+"."+getClass().getSimpleName().toLowerCase());
text=Messages.getMessage(Messages.cultureDemo+"."+getClass().getSimpleName().toLowerCase()+".text");
}
public String toString(){return name;}
String getText(){return text;}
public List<Spotlight> getSpotlights(FrameCulture cultureViewer){
return null;
}
protected Spotlight createSpotlight(java.awt.Component c){
int x=getX(c);
int y=getY(c);
int w=c.getSize().width;
int h=c.getSize().height;
return new Spotlight(x-((w/2)/2),
y-((h/2)/2),
w+(w/2),
h+(h/2));
}
protected int getX(java.awt.Component c){
if(c.getParent().getParent().getParent()==null){return c.getX();}
return c.getX()+getX(c.getParent());
}
protected int getY(java.awt.Component c){
if(c.getParent().getParent().getParent()==null){return c.getY();}
return c.getY()+getY(c.getParent());
}
}
/**
* Introduction
* @author alois_cochard@users.sf.net
*
*/
class Page1 extends DemoPage{}
/**
* Culture
* @author alois_cochard@users.sf.net
*
*/
class Page2 extends DemoPage{
public List<Spotlight> getSpotlights(FrameCulture cultureViewer){
List<Spotlight> spotlights=new ArrayList<Spotlight>();
spotlights.add(new Spotlight(getX(cultureViewer.getJTabbedPaneMain())-25,
getY(cultureViewer.getJTabbedPaneMain())-50,
150,
150));
return spotlights;
}
}
/**
* Hardwares
* @author alois_cochard@users.sf.net
*
*/
class Page3 extends DemoPage{
public List<Spotlight> getSpotlights(FrameCulture cultureViewer){
List<Spotlight> spotlights=new ArrayList<Spotlight>();
cultureViewer.getJTabbedPaneMain().setSelectedIndex(0);
spotlights.add(createSpotlight(cultureViewer.getJListHardwares()));
spotlights.add(createSpotlight(cultureViewer.getJButtonHardwaresAdd()));
spotlights.add(createSpotlight(cultureViewer.getJButtonHardwaresRemove()));
return spotlights;
}
}
/**
* Hardwares - Edit
* @author alois_cochard@users.sf.net
*
*/
class Page3_1 extends DemoPage{
public List<Spotlight> getSpotlights(FrameCulture cultureViewer){
List<Spotlight> spotlights=new ArrayList<Spotlight>();
cultureViewer.getJTabbedPaneMain().setSelectedIndex(0);
spotlights.add(createSpotlight(cultureViewer.getJPanelHardwaresDetails()));
return spotlights;
}
}
/**
* Plantations
* @author alois_cochard@users.sf.net
*
*/
class Page4 extends DemoPage{
public List<Spotlight> getSpotlights(FrameCulture cultureViewer){
List<Spotlight> spotlights=new ArrayList<Spotlight>();
cultureViewer.getJTabbedPaneMain().setSelectedIndex(1);
spotlights.add(createSpotlight(cultureViewer.getJPanelPlantationsHeader()));
return spotlights;
}
}
/**
* Plantations - Edit
* @author alois_cochard@users.sf.net
*
*/
class Page4_1 extends DemoPage{
public List<Spotlight> getSpotlights(FrameCulture cultureViewer){
List<Spotlight> spotlights=new ArrayList<Spotlight>();
cultureViewer.getJTabbedPaneMain().setSelectedIndex(1);
spotlights.add(new Spotlight(getX(cultureViewer.getJPanelPlantationsDetails())-(cultureViewer.getJPanelPlantationsDetails().getSize().width/8),
getY(cultureViewer.getJPanelPlantationsDetails())-(cultureViewer.getJPanelPlantationsDetails().getSize().height/8),
cultureViewer.getJPanelPlantationsDetails().getSize().width+(cultureViewer.getJPanelPlantationsDetails().getSize().width/4),
cultureViewer.getJPanelPlantationsDetails().getSize().height+(cultureViewer.getJPanelPlantationsDetails().getSize().height/4)));
return spotlights;
}
}