package com.drawbridge;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.File;
import java.util.LinkedList;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import com.drawbridge.dm.DMPanel;
import com.drawbridge.dm.DMSimplePanel;
import com.drawbridge.paper.PaperPanel;
import com.drawbridge.text.TextPanel;
import com.drawbridge.utils.AnalyticUtils;
import com.drawbridge.utils.ErrorPopup;
import com.drawbridge.utils.GraphicUtils;
import com.drawbridge.utils.ScrollTween;
import com.drawbridge.utils.ScrollTween.TransitionType;
import com.drawbridge.utils.Toolbar;
import com.drawbridge.utils.Utils;
import com.drawbridge.vl.VLPanel;
public class Activity extends JFrame
{
private static final long serialVersionUID = 1L;
private static Activity mActivity = null;
private JPanel mJPaneHolder = null;
private JScrollPane mScrollPane = null;
private Container mContent;
protected Toolbar mToolbar;
private final int mBorderPadding = 20;
private Dimension mSegmentSize = null;
private PaperPanel mPaper = null;
private DMPanel mDirectManipulation = null;
private DMSimplePanel mDirectManipulationSimple = null;
private VLPanel mVisualLanguage = null;
private TextPanel mTextLanguage = null;
private Popup tipWindow = null;
private ErrorPopup mErrorPopup = null;
public static final int mScreenSegmentNo = 2;
public Thread scrollThread = null;
LinkedList<SegmentDescription> mSegmentDescriptions = null;
public enum Version
{
// Versions for Ordering Study
VERSION_NOCONCRETE_TEXTFIRST, // Text + Visual + WebView (A)
VERSION_NOCONCRETE_VISUALFIRST, // Visual + Text + WebView (B)
VERSION_CONCRETE_TEXTFIRST, // Paper + DM + DMSimple + Text + Visual + WebView (C)
VERSION_CONCRETE_VISUALFIRST, // Paper + DM + DMSimple + Visual + Text + WebView (Default) (D)
// New Versions for Mario
VERSION_CONCRETE_JUSTVISUAL,
VERSION_CONCRETE_JUSTTEXTUAL;
};
// Controls the ordering of representations for studies
public final Version mVersion;
/**
* Allows retrieval of the singleton activity
*
* @return
*/
public static Activity getInstance()
{
if (mActivity == null)
{
mActivity = new Activity();
return mActivity;
}
return mActivity;
}
public void printFocusOwner()
{
Utils.out.println(this.getClass(), "Focus Owner:" + this.getFocusOwner().getClass().getSimpleName());
}
private Activity() {
super("DrawBridge");
String versionProperty = (String) Utils.loadDrawBridgeProperty("mVersion");
if(versionProperty != null && versionProperty.equals("askForVersion"))
mVersion = askUserForVersion();
else if(versionProperty != null)
mVersion = Version.valueOf(versionProperty);
else{ //Default
Utils.err.println(getClass(), "Unable to load properties - defaulting to VERSION_CONCRETE_VISUALFIRST");
mVersion = Version.VERSION_CONCRETE_VISUALFIRST;
}
AnalyticUtils.init(this, mVersion.name());
this.setDefaultCloseOperation(AnalyticUtils.getCloseOperation());
this.addWindowListener(AnalyticUtils.getWindowListener());
initUI();
}
private void initUI()
{
this.setName("DrawBridge");
this.setLocation(0, 0);
try
{
UIManager.setLookAndFeel(// UIManager.getCrossPlatformLookAndFeelClassName());
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e)
{
Utils.out.println(this.getClass(), "Error setting native LAF: " + e);
}
setLayout(new BorderLayout());
mContent = getContentPane();
mContent.setLayout(new BorderLayout());
//See if we can load an application icon
loadWindowsIcon();
/** Set the dimensions of the window to full screen **/
Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
Dimension screenDim = new Dimension(screenBounds.width, screenBounds.height);
this.setPreferredSize(new Dimension(screenDim.width, screenDim.height));
mJPaneHolder = new JPanel();
int segW = (int) ((double) (screenDim.width / mScreenSegmentNo) - ((mScreenSegmentNo + 1) * mBorderPadding));
int segH = (int) (screenDim.getHeight() - (2 * mBorderPadding));
mSegmentSize = new Dimension(segW, segH);
BoxLayout boxLayout = new BoxLayout(mJPaneHolder, BoxLayout.X_AXIS);
mJPaneHolder.setBorder(BorderFactory.createEmptyBorder(mBorderPadding, mBorderPadding, mBorderPadding, mBorderPadding));
mJPaneHolder.setLayout(boxLayout);
mScrollPane = new JScrollPane(mJPaneHolder);
mScrollPane.setEnabled(false);
mScrollPane.setBackground(Color.white);
mSegmentDescriptions = new LinkedList<SegmentDescription>();
mJPaneHolder.add(Box.createHorizontalStrut(mBorderPadding));
if (mVersion == Version.VERSION_CONCRETE_TEXTFIRST || mVersion == Version.VERSION_CONCRETE_VISUALFIRST || mVersion == Version.VERSION_CONCRETE_JUSTTEXTUAL || mVersion == Version.VERSION_CONCRETE_JUSTVISUAL)
{
/** Add paper panel **/
addPaperPanel();
/** Add direct manipulation panel **/
addDirectManipulationPanel();
/** Add simple direct manipulation panel **/
addDirectManipulationPanelSimple();
}
if (mVersion == Version.VERSION_CONCRETE_TEXTFIRST || mVersion == Version.VERSION_NOCONCRETE_TEXTFIRST)
{
/** Add text panel **/
addTextPanel();
/** Add visual language panel **/
addVisualLanguagePanel();
}
else if (mVersion == Version.VERSION_CONCRETE_VISUALFIRST || mVersion == Version.VERSION_NOCONCRETE_VISUALFIRST)
{
/** Add visual language panel **/
addVisualLanguagePanel();
/** Add text panel **/
addTextPanel();
}
else if(mVersion == Version.VERSION_CONCRETE_JUSTTEXTUAL){
addTextPanel();
}
else if(mVersion == Version.VERSION_CONCRETE_JUSTVISUAL){
addVisualLanguagePanel();
}
else{
throw new RuntimeException("Unknown version of DrawBridge");
}
/** Add WebView panel **/
addWebViewPanel();
mScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
mScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
// count the number of segments we have
int segmentCount = getSegmentNumber();
int jPaneWidth = (int) ((mSegmentSize.width * segmentCount) + ((segmentCount) * mBorderPadding));
int jPaneHeight = mSegmentSize.height - (3 * mBorderPadding) - ((Integer) UIManager.get("ScrollBar.width")).intValue();
mJPaneHolder.setPreferredSize(new Dimension(jPaneWidth, jPaneHeight));
/** Add the toolbar independently from the scrollPane **/
mToolbar = Toolbar.getInstance(this);
mToolbar.setLayout(new BoxLayout(mToolbar, BoxLayout.X_AXIS));
mToolbar.initToolbarComponents();
this.add(BorderLayout.NORTH, mToolbar);
this.add(BorderLayout.CENTER, mScrollPane);
this.pack();
mContent.setVisible(true);
}
private void loadWindowsIcon()
{
Image img = GraphicUtils.loadImageFromResource("/Assets/icon.png");
setIconImage(img);
}
private Version askUserForVersion()
{
String[] possibilities = {"Visual", "Textual"};
String s = (String)JOptionPane.showInputDialog(
this,
"Please select the option given to you by the researcher", "DrawBridge Mode",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"ham");
if(s == null)
System.exit(0);
switch(s){
// case "TF":
// return Version.VERSION_NOCONCRETE_TEXTFIRST;
// case "VF":
// return Version.VERSION_NOCONCRETE_VISUALFIRST;
// case "CTF":
// return Version.VERSION_CONCRETE_TEXTFIRST;
// case "CVF":
// return Version.VERSION_CONCRETE_VISUALFIRST;
case "Textual":
return Version.VERSION_CONCRETE_JUSTTEXTUAL;
case "Visual":
return Version.VERSION_CONCRETE_JUSTVISUAL;
}
Utils.err.println("ERROR: Unknown version. Defaulting.");
return Version.VERSION_CONCRETE_VISUALFIRST;
}
public void addWebViewPanel()
{
WebViewPanel mWebViewPanel = WebViewPanel.getInstance();
mWebViewPanel.setMinimumSize(mSegmentSize);
mWebViewPanel.setMaximumSize(mSegmentSize);
mJPaneHolder.add(mWebViewPanel);
mJPaneHolder.add(Box.createHorizontalStrut(mBorderPadding));
mSegmentDescriptions.add(new SegmentDescription("WebView", mWebViewPanel));
}
/**
* Method that changes the scroll view's viewport to focus on the selected index
*
* @param selectorID
* -- the index of the combined view of two segments.
*/
public boolean changeSegmentView(int selectorID)
{
if (mScrollPane != null && mSegmentSize != null)
{
// if(scrollThread == null || (scrollThread != null && !scrollThread.isAlive())){
int current = mScrollPane.getHorizontalScrollBar().getValue();
int target = (selectorID * ((mSegmentSize.width)));
ScrollTween tween = ScrollTween.getInstance(this, current, target, TransitionType.LINEAR);
// scrollThread = new Thread(tween);
SwingUtilities.invokeLater(tween);
tween.mEnabled = true;
// scrollThread.start();
return true;
// }
// else{ //if(scrollThread != null && scrollThread.isAlive()){
//Do nothing
// return false;
// }
}
return false;
}
public void addPaperPanel()
{
mPaper = PaperPanel.getInstance();
mPaper.setMinimumSize(mSegmentSize);
mPaper.setMaximumSize(mSegmentSize);
mJPaneHolder.add(mPaper);
mJPaneHolder.add(Box.createHorizontalStrut(mBorderPadding));
mSegmentDescriptions.add(new SegmentDescription("Paper", mPaper));
}
public void addTextPanel()
{
mTextLanguage = TextPanel.getInstance();
mTextLanguage.setMinimumSize(mSegmentSize);
mTextLanguage.setMaximumSize(mSegmentSize);
mJPaneHolder.add(mTextLanguage);
mJPaneHolder.add(Box.createHorizontalStrut(mBorderPadding));
mSegmentDescriptions.add(new SegmentDescription("Text", mTextLanguage));
}
public void addDirectManipulationPanel()
{
mDirectManipulation = DMPanel.getInstance();
// if(mPaper != null){
// mDirectManipulation.registerOnPaperPanelUpdates(mPaper);
// }
mDirectManipulation.setMinimumSize(mSegmentSize);
mDirectManipulation.setMaximumSize(mSegmentSize);
mJPaneHolder.add(mDirectManipulation);
mJPaneHolder.add(Box.createHorizontalStrut(mBorderPadding));
mSegmentDescriptions.add(new SegmentDescription("DM", mDirectManipulation));
}
public void addDirectManipulationPanelSimple()
{
mDirectManipulationSimple = DMSimplePanel.getInstance();
mDirectManipulationSimple.setMinimumSize(mSegmentSize);
mDirectManipulationSimple.setMaximumSize(mSegmentSize);
mJPaneHolder.add(mDirectManipulationSimple);
mJPaneHolder.add(Box.createHorizontalStrut(mBorderPadding));
mSegmentDescriptions.add(new SegmentDescription("DMSimple", mDirectManipulationSimple));
// TODO just changed it to setMinimumSize which helps
}
public void addVisualLanguagePanel()
{
mVisualLanguage = VLPanel.getInstance();
mVisualLanguage.setMinimumSize(mSegmentSize);
mVisualLanguage.setMaximumSize(mSegmentSize);
mJPaneHolder.add(mVisualLanguage);
mJPaneHolder.add(Box.createHorizontalStrut(mBorderPadding));
mSegmentDescriptions.add(new SegmentDescription("VL", mVisualLanguage));
}
public int getSegmentNumber()
{
return mSegmentDescriptions.size();
}
public LinkedList<SegmentDescription> getSegmentDescriptions()
{
return mSegmentDescriptions;
}
public Toolbar getToolbar(){
return mToolbar;
}
public class SegmentDescription
{
public String mName;
public JPanel mSegment;
public SegmentDescription(String name, JPanel segment) {
mName = name;
mSegment = segment;
}
@Override
public boolean equals(Object segDesc)
{
SegmentDescription seg = (SegmentDescription) segDesc;
if (seg.mName.equals(this.mName) && seg.mSegment.equals(this.mSegment))
return true;
return false;
}
}
public JScrollPane getScrollPane()
{
return mScrollPane;
}
public void reloadApplication(File file)
{
this.removeAll();
this.validate();
}
public void showErrorPopup(String title, String description, Point point)
{
if (tipWindow != null && mErrorPopup != null)
{
if (mErrorPopup.equals(new ErrorPopup(title, description)))
{
// Do nothing
}
else
{
mErrorPopup.setVisible(false);
tipWindow.hide();
PopupFactory popupFactory = PopupFactory.getSharedInstance();
mErrorPopup = new ErrorPopup(title, description);
tipWindow = popupFactory.getPopup(this, mErrorPopup, point.x, point.y - (mErrorPopup.getHeight() / 2));
tipWindow.show();
}
}
else
{
PopupFactory popupFactory = PopupFactory.getSharedInstance();
mErrorPopup = new ErrorPopup(title, description);
tipWindow = popupFactory.getPopup(this, mErrorPopup, point.x, point.y - (mErrorPopup.getHeight() / 2));
tipWindow.show();
}
}
public void hideTipWindow()
{
if (tipWindow != null)
{
tipWindow.hide();
tipWindow = null;
}
}
}