Code Overview

This topic applies to Java version only.

Now the plug-in environment is configured and we can look at the code itself.

At this point in time the project contains the following classes:

Activator class is called to start and stop the plug-in. It is responsible for managing its lifecycle. We will use it to initialize and clean up db4o resources.

SampleAction is a class that performs the action specified in the action set in the manifest file. It can be used to specify the behavior on the action. We will use it to call a custom dialog for memo viewing and editing.

From the said above we can see that we will need 2 more classes:

These 2 classes are very basic and are not specific to OSGI environment. Please, review their code below:

Db4oProvider.java
01package memoplugin; 02 03import com.db4o.ObjectContainer; 04import com.db4o.osgi.Db4oService; 05/* 06 * This class is used to store db4o_osgi service instance and 07 * provide db4o services on request. 08 */ 09public class Db4oProvider { 10 11 private static ObjectContainer _db; 12 private static String FILENAME = "sample.db4o"; 13 14 public static void Initialize(Db4oService db4oService){ 15 _db = db4oService.openFile(FILENAME); 16 } 17 18 public static ObjectContainer database(){ 19 return _db; 20 } 21 22 public static void UnInitialize(){ 23 _db.close(); 24 } 25 26}
 

DataDialog.java
001package memoplugin.ui; 002 003import java.util.ArrayList; 004 005import org.eclipse.jface.dialogs.Dialog; 006import org.eclipse.jface.dialogs.IDialogConstants; 007import org.eclipse.jface.dialogs.MessageDialog; 008import org.eclipse.swt.SWT; 009import org.eclipse.swt.layout.GridData; 010import org.eclipse.swt.widgets.Button; 011import org.eclipse.swt.widgets.Composite; 012import org.eclipse.swt.widgets.Control; 013import org.eclipse.swt.widgets.Label; 014import org.eclipse.swt.widgets.List; 015import org.eclipse.swt.widgets.Shell; 016import org.eclipse.swt.widgets.Text; 017 018import memoplugin.Db4oProvider; 019 020import com.db4o.ObjectSet; 021 022public class DataDialog extends Dialog { 023 private static int ID_ADD = 100; 024 private static int ID_DELETE = 101; 025 private Shell _shell; 026 /** 027 * The title of the dialog. 028 */ 029 private String title; 030 031 /** 032 * The message to display, or <code>null</code> if none. 033 */ 034 private String message; 035 036 /** 037 * The input value; the empty string by default. 038 */ 039 private String value = "";//$NON-NLS-1$ 040 041 042 /** 043 * Add button widget. 044 */ 045 private Button addButton; 046 047 /** 048 * Delete button widget. 049 */ 050 private Button deleteButton; 051 052 /** 053 * Input text widget. 054 */ 055 private Text text; 056 057 /** 058 * List widget. 059 */ 060 private List list; 061 062 063 public DataDialog(Shell parentShell, String dialogTitle, 064 String dialogMessage, String initialValue) { 065 super(parentShell); 066 this.title = dialogTitle; 067 message = dialogMessage; 068 if (initialValue == null) { 069 value = "";//$NON-NLS-1$ 070 } else { 071 value = initialValue; 072 } 073 } 074 075 /* 076 * (non-Javadoc) 077 * 078 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) 079 */ 080 protected void configureShell(Shell shell) { 081 super.configureShell(shell); 082 _shell = shell; 083 if (title != null) { 084 shell.setText(title); 085 } 086 } 087 088 /* 089 * Clears the database before adding new data 090 */ 091 private void clearDb(){ 092 ObjectSet result = Db4oProvider.database().get(ArrayList.class); 093 while (result.hasNext()){ 094 Db4oProvider.database().delete(result.next()); 095 } 096 } 097 098 /* 099 * (non-Javadoc) 100 * Makes sure that all the data is saved to the 101 * database before closing the dialog 102 */ 103 protected void handleShellCloseEvent() { 104 clearDb(); 105 ArrayList data = new ArrayList(); 106 for (int i=0; i < list.getItemCount(); i++){ 107 data.add(list.getItem(i)); 108 } 109 Db4oProvider.database().set(data); 110 Db4oProvider.database().commit(); 111 Db4oProvider.database().ext().purge(ArrayList.class); 112 super.handleShellCloseEvent(); 113 } 114 115 /* 116 * Button events handler 117 */ 118 protected void buttonPressed(int buttonId) { 119 if (buttonId == ID_ADD) { 120 value = text.getText(); 121 list.add(value); 122 } else if (buttonId == ID_DELETE){ 123 int selectedId = list.getSelectionIndex(); 124 if (selectedId == -1){ 125 new MessageDialog(_shell, "Error", 126 null, "No item selected", MessageDialog.ERROR, 127 new String[]{"Ok"}, 0).open(); 128 } else { 129 list.remove(selectedId); 130 } 131 value = null; 132 } else { 133 super.buttonPressed(buttonId); 134 } 135 } 136 137 /* 138 * (non-Javadoc) 139 * 140 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) 141 */ 142 protected void createButtonsForButtonBar(Composite parent) { 143 // create Add and Delete buttons by default 144 addButton = createButton(parent, ID_ADD, 145 "Add", true); 146 createButton(parent, ID_DELETE, 147 "Delete", false); 148 //do this here because setting the text will set enablement on the ok 149 // button 150 text.setFocus(); 151 if (value != null) { 152 text.setText(value); 153 text.selectAll(); 154 } 155 } 156 157 /* 158 * (non-Javadoc) Creates the visual dialog representation 159 */ 160 protected Control createDialogArea(Composite parent) { 161 // create composite 162 Composite composite = (Composite) super.createDialogArea(parent); 163 // create message 164 if (message != null) { 165 Label label = new Label(composite, SWT.WRAP); 166 label.setText(message); 167 GridData gridData = new GridData(GridData.GRAB_HORIZONTAL 168 | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL 169 | GridData.VERTICAL_ALIGN_CENTER); 170 gridData.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); 171 label.setLayoutData(gridData); 172 label.setFont(parent.getFont()); 173 } 174 text = new Text(composite, SWT.SINGLE | SWT.BORDER); 175 text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL 176 | GridData.HORIZONTAL_ALIGN_FILL)); 177 178 list = new List(composite, SWT.SINGLE|SWT.H_SCROLL|SWT.V_SCROLL); 179 GridData gridData = new GridData(SWT.FILL,SWT.FILL, true, true); 180 gridData.heightHint = 50; 181 list.setLayoutData(gridData); 182 ObjectSet result = Db4oProvider.database().query(ArrayList.class); 183 if (result.size() != 0){ 184 ArrayList data = (ArrayList)result.next(); 185 String[] items = new String[data.size()]; 186 for (int i=0; i < data.size(); i++){ 187 items[i] = (String)data.get(i); 188 } 189 list.setItems(items); 190 } 191 192 193 applyDialogFont(composite); 194 return composite; 195 } 196 197 198 /** 199 * Returns the string typed into this input dialog. 200 * 201 * @return the input string 202 */ 203 public String getValue() { 204 return value; 205 } 206 207}

In order to call the above-mentioned DataDialog we will need to modify the generated run method in SampleAction class:

SampleAction.java: run
01/** 02 * The action has been activated. The argument of the 03 * method represents the 'real' action sitting 04 * in the workbench UI. 05 * @see IWorkbenchWindowActionDelegate#run 06 */ 07 public void run(IAction action) { 08 /* 09 * Call DataDialog to view and edit memo notes 10 */ 11 DataDialog d = new DataDialog(window.getShell(), "db4o-osgi", "Enter an item to add to the list:",null); 12 d.open(); 13 }