package virtualnewsreader;
/**
*
* @author kpham
*/
//import chrriis.dj.nativeswing.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import java.awt.Color;
import java.awt.event.ComponentEvent;
import java.io.IOException;
import java.awt.Component;
import java.awt.Insets;
import java.awt.event.ComponentListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.text.StyledEditorKit;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.*;
import org.jvnet.substance.SubstanceLookAndFeel;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ScrollPaneConstants;
public class TabManager extends JTabbedPane{
TabBrowserControl browserTab;
JWebBrowser webBrowser;
String WebTitle = "Web Browser";
JMenu WebBrowserMenu = null;
int MaxEditorWidth = 800;
int MinEditorWidth = 800;
public TabManager()
{
this.putClientProperty(SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY,Boolean.TRUE);
this.addContainerListener(new java.awt.event.ContainerAdapter() {
@Override
public void componentRemoved(java.awt.event.ContainerEvent evt) {
RemoveBrowserMenu();
}
});
this.addComponentListener(new ComponentListener(){
public void componentResized(ComponentEvent arg0) {
onResize();
}
public void componentMoved(ComponentEvent arg0) {}
public void componentShown(ComponentEvent arg0) {}
public void componentHidden(ComponentEvent arg0) {}
});
}
/**
* get the webBrowser component
* @return JWebBrowser;
*/
public JWebBrowser getWebBrowser()
{
return this.webBrowser;
}
/**
* get the web browser tab control;
* @return TabBrowserControl
*/
public TabBrowserControl getBrowserTabCtrl(){
return browserTab;
}
/**
* Get the text from the tab manager, return null if nothing.
*
* @return
* 1) if the current tab is currently selected, get the selected text from the web browser
* 2) if a document tab is selected, get the text and return it.
*/
public String getTextOnCurrentTab(){
String sText = null;
Component CurrComp = this.getSelectedComponent();
if(CurrComp == null){
JOptionPane.showMessageDialog(null,"Please create a new Document or open the Web Browser tab", "Nothing to Speak", JOptionPane.ERROR_MESSAGE);
return null;
}
//if the current tab is the webbrowser
if(CurrComp.getName().startsWith("Web")){
sText = this.getBrowserTabCtrl().getSelectedText();
if(sText == null){
JOptionPane.showMessageDialog(null,"Please select some text from the webpage", "Nothing to Speak", JOptionPane.ERROR_MESSAGE);
return null;
}
}
else if(!CurrComp.getName().startsWith("Web")){
JScrollPane scrPane = (JScrollPane) CurrComp;
JEditorPane myCurrDoc = (JEditorPane) scrPane.getViewport().getComponent(0);
sText = myCurrDoc.getText();
if(sText == null || sText.equals("")){
JOptionPane.showMessageDialog(null,"Please type some text into the Editor Area", "Nothing to Speak", JOptionPane.ERROR_MESSAGE);
return null;
}
}
return sText;
}
private void onResize() {
for (int i = 0; i < getComponentCount(); i++){
Component comp = getComponentAt(i);
//if not webBrowser we modify all text component
if(!comp.getName().startsWith("Web")){
JScrollPane scrollPane = (JScrollPane) comp;
JEditorPane myDoc = (JEditorPane) scrollPane.getViewport().getComponent(0);
int margin = (getWidth() - MaxEditorWidth) / 2;
if(margin < 0)
margin = 0;
myDoc.setMargin(new Insets(0,margin,0,margin));
myDoc.setSize(MaxEditorWidth,Short.MAX_VALUE);
// if (getWidth() > this.MaxEditorWidth)
// {
// scrollPane.remove(scrollPane.getHorizontalScrollBar());
// }
// else
// {
// scrollPane.setHorizontalScrollBar(new JScrollBar(HORIZONTAL));
// scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// scrollPane.getViewport().getComponent(0).setMaximumSize(new Dimension(MaxEditorWidth, Short.MAX_VALUE));
// System.out.println("add scroll");
// }
}
}
}
/**
* Asking to see if this document name already exist on the Tab Pane.
* only use when try to create a new Tab.
*
* @param name
* @return
*/
private boolean isDocNameExist(String name){
Component[] compArr = getComponents();
//for every tab find a matching name... if found one return true;
for(int i =0; i<compArr.length; i++){
Component cmp = getComponentAt(i);
if(name.toLowerCase().equals(cmp.getName().toLowerCase())){
return true;
}
}
return false; //Name is not exist
}
/**
* Adding new document Tab to the TabPane.
* @throws java.io.IOException
*/
public void addDocumentTab() throws IOException
{
String docName = JOptionPane.showInputDialog(null, "Please Enter Document Name: ","VirtualNewsReader", 1);
if (docName != null) { //if Hit OK
do{
if(docName.equals("")){
JOptionPane.showMessageDialog(null, "Document name cannot be blank", "VirtualNewsReader", 1);
}
else if(isDocNameExist(docName)){
JOptionPane.showMessageDialog(null, "Document cannot have the same Name", "VirtualNewsReader", 1);
}
else {
CreateDocumentComponent(docName);
break;
}
docName = JOptionPane.showInputDialog(null, "Please Enter Document Name: ","VirtualNewsReader", 1);
}while(docName != null);
onResize();
}
}
/**
* Should Only be called by addDocumentTab
* @param Name
*/
private void CreateDocumentComponent(String Name){
final JEditorPane myDoc = new JEditorPane();
myDoc.setEditorKit(new StyledEditorKit());
myDoc.setSelectedTextColor(Color.BLUE);
myDoc.setOpaque(true);
JScrollPane scrollPane = new JScrollPane(myDoc);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setName(Name);
this.add(scrollPane);
this.setSelectedComponent(scrollPane);
}
/**
* Adding the Browser component to the TabPane
*/
public void addBrowserTab()
{
if(getTabCount() == 0 || getComponentAt(0) != webBrowser)
{
browserTab = new TabBrowserControl(this);
webBrowser = browserTab.CreateBrowserTab();
webBrowser.navigate("http://google.com");
webBrowser.setName("WebBrowser");
AddBrowserMenu();
insertTab(WebTitle, null, webBrowser, "A web Browser", 0);
}
setSelectedComponent(webBrowser);
}
/**
* Remove the Browser's Menu from the application menu.
* when the user close the Browser Tab.
*/
private void RemoveBrowserMenu()
{
//If the tab removed was the webbrowser, remove the webbrowser menu from the main menu
if(getTabCount() == 0 || getComponentAt(0) != webBrowser)
{
if(WebBrowserMenu != null) //only delete the menu if we know the menu is existed.
{
VirtualNewsReaderView frameView =
(VirtualNewsReaderView) ( (VirtualNewsReaderApp) SingleFrameApplication.getInstance()).getMainView();
JMenuBar appMenuBar = frameView.getMenuBar();
appMenuBar.getMenu(appMenuBar.getComponentIndex(WebBrowserMenu)).setVisible(false);
appMenuBar.remove(appMenuBar.getMenu(appMenuBar.getComponentIndex(WebBrowserMenu)));
WebBrowserMenu = null;
//reset the webbrowser
webBrowser = null;
}
}
}
/**
* When the user create the Browser Tab.
* we adding the browser menu into the Application Menu
*/
private void AddBrowserMenu()
{
VirtualNewsReaderApp MyApp = (VirtualNewsReaderApp) SingleFrameApplication.getInstance();
VirtualNewsReaderView frameView = (VirtualNewsReaderView)MyApp.getMainView();
if(WebBrowserMenu == null) //only create the menu when its not create yet
{
WebBrowserMenu = new JMenu();
WebBrowserMenu.setText("Web Browser");
WebBrowserMenu.setName("WebBrowserMenu");
for(int i = 0; i<=webBrowser.getMenuBar().getMenuCount();i++)
{
WebBrowserMenu.add(webBrowser.getMenuBar().getMenu(0));
}
frameView.getMenuBar().add(WebBrowserMenu,1);
}
}
}