Opening MS Word file in eclipse - eclipse

I am currently using eclipse Kepler as an ID for my development work.I would like to know how can i open MS word file and view same at eclipse ID. Thanks in advance.

You should use OleClientSite(...,File).
public class WordSample {
private Shell shell;
private OleFrame frame;
private OleClientSite site;
public WordSample() {
Display display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(800, 600);
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);
MenuItem fileMenu = new MenuItem(bar, SWT.CASCADE);
fileMenu.setText("&File");
Menu menuFile = new Menu(fileMenu);
fileMenu.setMenu(menuFile);
MenuItem menuOpen = new MenuItem(menuFile, SWT.CASCADE);
menuOpen.setText("&Open");
menuOpen.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
fileDialog.setFilterExtensions(new String[] {"*.doc"});
String doc = fileDialog.open();
if (doc != null && !doc.equals("")) {
openDocument(doc);
}
}
});
frame = new OleFrame(shell, SWT.NONE);
frame.setFileMenus(new MenuItem[] {fileMenu});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
private void openDocument(String doc) {
if (site != null && !site.isDisposed()) site.dispose();
site = new OleClientSite(frame, SWT.NONE, "Word.Document", new
File(doc));
site.doVerb(OLE.OLEIVERB_SHOW);
}
public static void main(String[] args) {
WordSample sample = new WordSample();
}
}

Related

want to create drop down list (Combo box viewer) in TreeColumn in SWT

I am using Tree and in this tree I have a five treecolumn. Also create two treeItem one is parent and other child, put their values in treecolumn by programatically. Now I need a dropdown List(Combobox) in each tree column(except first one) to view the list data. Currently getting only single value. Please see the below code to get tree item values editable in treecolumn.
private void editTreeTable(final Tree table){
final TreeEditor editor = new TreeEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseUp(final MouseEvent e) {
final Control oldEditor = editor.getEditor();
if (oldEditor != null) {
oldEditor.dispose();
}
final Point p = new Point(e.x, e.y);
final TreeItem item = table.getItem(p);
if (item == null) {
return;
}
for (int i = 1; i < table.getColumnCount(); ++i) {
if (item.getBounds(i).contains(p)) {
final int columnIndex = i;
// The control that will be the editor must be a
final Text newEditor = new Text(table, SWT.NONE);
newEditor.setText(item.getText(columnIndex ));
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(final ModifyEvent e) {
final Text text = (Text) editor.getEditor();
editor.getItem().setText(columnIndex , text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
editor.setEditor(newEditor, item, columnIndex );
}
}
}
});
}
Now find the below code to get the tree item value from API
private void createTestSuiteTable( final Tree table)
{
//Dispose all elements
TreeItem items[] = table.getItems();
for(int i=0;i<items.length;i++)
{
items[i].dispose();
}
TSGson tsGsons[] = TestSuiteAPIHandler.getInstance().getAllTestSuites();
boolean checked=false;
for (TSGson tsGson : tsGsons)
{
parentTestSuite = new TreeItem(table, SWT.NONE|SWT.MULTI);
parentTestSuite.setText(new String[] { "" +tsGson.tsName, "", "","","","" });
parentTestSuite.setData("EltType","TESTSUITE");
if(tsGson.tsTCLink==null)
continue;
for(TSTCGson tsTCGson : tsGson.tsTCLink)
{
TreeItem trtmTestcases = new TreeItem(parentTestSuite, SWT.NONE|SWT.MULTI);
trtmTestcases.setText(new String[] {tsTCGson.tcName,
tsTCGson.tcParams.get(0)!=null ?tsTCGson.tcParams.get(0).tcparamValue:"",
tsTCGson.tcParams.get(1)!=null ?tsTCGson.tcParams.get(1).tcparamValue:"",
tsTCGson.tcParams.get(2)!=null ?tsTCGson.tcParams.get(2).tcparamValue:"",
"local",
tsTCGson.tcParams.get(4)!=null ?tsTCGson.tcParams.get(4).tcparamValue:"" });
trtmTestcases.setData("EltType","TESTCASE");
table.setSelection(parentTestSuite);
if(checked)
{
trtmTestcases.setChecked(checked);
}
}
}
}
Find the below code for tree column creation in SWT
localHostTable = new Tree(composite_2,SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION | SWT.VIRTUAL);
localHostTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
localHostTable.setLinesVisible(true);
localHostTable.setHeaderVisible(true);
TreeColumn trclmnNewColumn_1 = new TreeColumn(localHostTable, SWT.NONE);
trclmnNewColumn_1.setWidth(113);
trclmnNewColumn_1.setText("TestSuite/TestCase");
TreeColumn trclmnColumn_5 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_5.setWidth(73);
trclmnColumn_5.setText("Exe_Platform");
TreeColumn trclmnColumn_6 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_6.setWidth(77);
trclmnColumn_6.setText("Exe_Type");
TreeColumn trclmnColumn_7 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_7.setWidth(85);
trclmnColumn_7.setText("Run_On");
TreeColumn trclmnColumn_8 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_8.setWidth(81);
trclmnColumn_8.setText("Thread-Count");
final TreeColumn trclmnColumn_9 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_9.setWidth(97);
trclmnColumn_9.setText("Column5");
please suggest
Since there's nothing in your question about Combo or CCombo controls, I can't help you troubleshoot an issue. I also am not going to write your code for you, but I can try to point you in the right direction with a short example.
Yes, i want the combo to always be visible.
You can still use a TreeEditor to accomplish this, and it will actually be simpler than the code snippet you posted with the MouseListener.
Create the CCombo (or Combo) as you would in any other situation, and use TreeEditor.setEditor(...) methods to specify that the CCombo control should be displayed in that cell:
// ...
final CCombo combo = new CCombo(tree, SWT.NONE);
final TreeEditor editor = new TreeEditor(tree);
editor.setEditor(combo, item, 1);
// ...
Full MCVE:
public class TreeComboBoxTest {
private final Display display;
private final Shell shell;
public TreeComboBoxTest() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout());
final Tree tree = new Tree(shell, SWT.BORDER | SWT.VIRTUAL | SWT.FULL_SELECTION);
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
tree.setLinesVisible(true);
tree.setHeaderVisible(true);
final TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
column1.setWidth(75);
column1.setText("Column 1");
final TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
column2.setWidth(75);
column2.setText("Column 2");
final TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText(0, "Hello");
final CCombo combo = new CCombo(tree, SWT.NONE);
combo.setItems(new String[] { "Item 1", "Item 2", "Item 3" });
final TreeEditor editor = new TreeEditor(tree);
editor.setEditor(combo, item, 1);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
// Optional, but allows you to get the current value by calling
// item.getText() instead of going through the TreeEditor and
// calling ((CCombo) editor.getEditor()).getText()
combo.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(final SelectionEvent e) {
item.setText(1, combo.getText());
}
});
}
public void run() {
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(final String... args) {
new TreeComboBoxTest().run();
}
}
Note the SelectionListener added to the CCombo. Even though you've used the TreeEditor, if you call item.getText(index), it will return an empty String because setText(...) has not been called. By calling setText(...) in the listener, you won't have to go through the TreeEditor to get the value.
So you can call item.getText(index) instead of ((CCombo) editor.getEditor()).getText().

Nebula GridViewer Selection not working

I am using Nebula Grid to display an excel sheet in my RCP application. I have an requirement to select all rows. My code is as follows:
private void addKeyListener() {
this.gv.getGrid().addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent arg0) {
}
#Override
public void keyReleased(KeyEvent e) {
if(e.stateMask==SWT.CTRL && e.keyCode =='a'){
ArrayList al = (ArrayList) gv.getInput();
//System.out.println("ctrl+c pressed");
gv.setSelection(new StructuredSelection(al.toArray()),true);
//gv.getGrid().setSelection(0, al.size()-1);
//gv.getGrid().selectAll();
//gv.getGrid().setSelection(new int[]{1,2});
//gv.getGrid().setSelection(1);
//gv.getGrid().setSelectionEnabled(true);
//gv.getGrid().select(new int[]{1,2,3});
//gv.refresh();
}
but this is not working. what am I missing ?
You have not mentioned what type of selection behaviour is being used by the grid in your code. i.e, Row or Cell Selection, single or multiple selections (SWT.SINGLE & SWT.MULTI styles).
Setting the style to SWT.MULTI when initializing the Grid seems to do the trick.
Here is the snippet that works for me.
public class GridSnippet {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
final Grid grid = new Grid(shell,SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
grid.setHeaderVisible(true);
// grid.setCellSelectionEnabled(true);
GridColumn column = new GridColumn(grid,SWT.NONE);
column.setText("Column 1");
column.setWidth(100);
GridItem item1 = new GridItem(grid,SWT.NONE);
item1.setText("First Item");
GridItem item2 = new GridItem(grid,SWT.NONE);
item2.setText("Second item");
GridItem item3 = new GridItem(grid,SWT.NONE);
item3.setText("Third Item");
grid.addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent arg0) {
}
#Override
public void keyReleased(KeyEvent e) {
if(e.stateMask==SWT.CTRL && e.keyCode =='a'){
System.out.println("ctrl+c pressed");
grid.selectAll();
}
}
});
shell.setSize(200,200);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

JTextArea, JMenuBar, JMenu, JMenuItem not showing up

I am quite new to Java so I need some help. I am trying to make a Notepad application.
The problem is that none of my menus or textfield is showing up. I cannot figure out what the problem is. Please help.
public class NotePad extends JFrame implements ActionListener {
private JTextArea txtArea;
private JMenuBar mnuBar;
private JMenu mnyFile, mnyFormat, mnyEdit, mnyHelp;
private JMenuItem openFile, saveFile, exit, textWrap, noTextWrap, clear, abtNotepad;
public NotePad() {
setTitle("NOTEPAD");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//Tekstboks
txtArea = new JTextArea();
//MenyBar
mnuBar = new JMenuBar();
//Meny
mnyFile = new JMenu("File");
mnyFormat = new JMenu("Format");
mnyEdit = new JMenu("Edit");
mnyHelp = new JMenu("Help");
//UnderMeny
openFile = new JMenuItem("Open");
saveFile = new JMenuItem("Save");
exit = new JMenuItem("Exit");
textWrap = new JMenuItem("Text Wrap");
noTextWrap = new JMenuItem("No Text Wrap");
clear = new JMenuItem("Clear");
abtNotepad = new JMenuItem("About Notepad");
add(txtArea);
add(mnuBar);
add(mnyFile);
add(mnyFormat);
add(mnyEdit);
add(mnyHelp);
add(openFile);
add(saveFile);
add(exit);
add(textWrap);
add(noTextWrap);
add(clear);
add(abtNotepad);
setJMenuBar(mnuBar);
setVisible(true);
}
public static void main(String[] args) {
new NotePad();
}
public void actionPerformed(ActionEvent e) {
}
}
Your constructor should look something like:
public NotePad() {
setTitle("NOTEPAD");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
txtArea = new JTextArea();
mnuBar = new JMenuBar();
mnyFile = new JMenu("File");
mnyFormat = new JMenu("Format");
mnyEdit = new JMenu("Edit");
mnyHelp = new JMenu("Help");
openFile = new JMenuItem("Open");
saveFile = new JMenuItem("Save");
exit = new JMenuItem("Exit");
textWrap = new JMenuItem("Text Wrap");
noTextWrap = new JMenuItem("No Text Wrap");
clear = new JMenuItem("Clear");
abtNotepad = new JMenuItem("About Notepad");
mnuBar.add(mnyFile);
mnuBar.add(mnyFormat);
mnuBar.add(mnyEdit);
mnuBar.add(mnyHelp);
mnyFile.add(openFile);
mnyFile.add(saveFile);
mnyFile.add(exit);
mnyFormat.add(textWrap);
mnyFormat.add(noTextWrap);
mnyEdit.add(clear);
mnyHelp.add(abtNotepad);
setJMenuBar(mnuBar);
add(txtArea);
setVisible(true);
}
Otherwise you are overriding each component you add to BorderLayout.

JTable Not Displaying from Window Builder Eclipse

I've just started to use the Eclipse Window Builder plugin to create a JFrame for my program but when adding a JTable it's just not showing up. The program acts like it's there but won't show it.
public class Stock extends JFrame {
private static final long serialVersionUID = -4904110593143929972L;
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Stock frame = new Stock();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Stock() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Add");
menuBar.add(mnNewMenu);
JMenu mnNewMenu_1 = new JMenu("Delete");
menuBar.add(mnNewMenu_1);
JMenu mnNewMenu_2 = new JMenu("Edit");
menuBar.add(mnNewMenu_2);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
table = new JTable();
table.setBounds(10, 230, 414, -221);
contentPane.add(table);
loadTable();
}
public void loadTable() {
String[] columnNames = {"Key",
"Name",
"Quantity",
"Price per unit"};
Object[][] data = {
{"1","Name","10","�2.40"}};
table = new JTable(data, columnNames);
contentPane.add(table);
}
}
Like I said, I've only just started with Window Builder so it's probably just simple but I can't figure it out.
Sorry the issue was this line, "
contentPane.setLayout(null);" no idea how it got there. Ah well least its working :)

Trying to set the size/location of a button in a tab for SWT

This should be pretty simple but this is the first time I've worked with SWT. This is what I have so far.
public class TabsTest {
private Shell shell;
private CTabFolder folder;
public TabsTest(Display display){
shell = new Shell(display);
shell.setText("TabsTest");
shell.setLayout(new FillLayout());
CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
folder.setUnselectedCloseVisible(false);
folder.setSimple(false);
initUI(folder);
shell.pack();
shell.setBounds(500, 500, 400, 500);
shell.open ();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
public void initUI(CTabFolder folder){
CTabItem NFL = new CTabItem(folder, SWT.NONE);
NFL.setText("NFL Bets");
Button okButton = new Button(folder, SWT.PUSH);
okButton.setText("OK");
okButton.setSize(10,10);
NFL.setControl(okButton);
CTabItem NBA = new CTabItem(folder,SWT.NONE);
NBA.setText("NBA Bets");
CTabItem CFB = new CTabItem(folder,SWT.NONE);
CFB.setText("CFB Bets");
folder.setSize(800,500);
}
public static void main (String [] args) {
Display display = new Display();
new TabsTest(display);
display.dispose();
}
}
What this currently gives me is this....
How would I make this a small button in the bottom right corner? Or just in general make it smaller and move it somewhere.
Since you are using a FillLayout the control takes up the entire space available. What you need is a different kind of a layout. I will suggest you to read this article, it will be a good start.
I generally prefer GridLayout as it is quite easy to use and it fulfills most needs.
Edited: Modifying your code to use GridLayout
public class TabsTest {
private Shell shell;
private CTabFolder folder;
public TabsTest(Display display) {
shell = new Shell(display);
shell.setText("TabsTest");
shell.setLayout(new GridLayout());
CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
folder.setUnselectedCloseVisible(false);
folder.setSimple(false);
folder.setLayoutData(new GridData(GridData.FILL_BOTH));
initUI(folder);
shell.pack();
shell.setBounds(500, 500, 400, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public void initUI(CTabFolder folder) {
CTabItem NFL = new CTabItem(folder, SWT.NONE);
NFL.setText("NFL Bets");
Composite nflParent = new Composite(folder, SWT.NONE);
nflParent.setBackground(folder.getDisplay().getSystemColor(SWT.COLOR_BLUE));
nflParent.setLayout(new GridLayout());
Button okButton = new Button(nflParent, SWT.PUSH);
okButton.setText("OK");
GridData gd = new GridData();
gd.verticalAlignment = GridData.END;
gd.horizontalAlignment = GridData.END;
gd.grabExcessHorizontalSpace = true;
gd.grabExcessVerticalSpace = true;
okButton.setLayoutData(gd);
NFL.setControl(nflParent);
CTabItem NBA = new CTabItem(folder, SWT.NONE);
NBA.setText("NBA Bets");
CTabItem CFB = new CTabItem(folder, SWT.NONE);
CFB.setText("CFB Bets");
folder.setSize(800, 500);
}
public static void main(String[] args) {
Display display = new Display();
new TabsTest(display);
display.dispose();
}
}