|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Intel Corporation |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of the Intel Corporation nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * |
| 28 | + */ |
| 29 | + |
| 30 | +package org.sofproject.gst.json; |
| 31 | + |
| 32 | +import java.io.BufferedWriter; |
| 33 | +import java.io.File; |
| 34 | +import java.io.FileWriter; |
| 35 | +import java.io.IOException; |
| 36 | + |
| 37 | +import org.eclipse.core.runtime.CoreException; |
| 38 | +import org.eclipse.swt.SWT; |
| 39 | +import org.eclipse.swt.events.SelectionAdapter; |
| 40 | +import org.eclipse.swt.events.SelectionEvent; |
| 41 | +import org.eclipse.swt.graphics.Rectangle; |
| 42 | +import org.eclipse.swt.layout.GridData; |
| 43 | +import org.eclipse.swt.layout.GridLayout; |
| 44 | +import org.eclipse.swt.widgets.Button; |
| 45 | +import org.eclipse.swt.widgets.Combo; |
| 46 | +import org.eclipse.swt.widgets.Display; |
| 47 | +import org.eclipse.swt.widgets.Label; |
| 48 | +import org.eclipse.swt.widgets.MessageBox; |
| 49 | +import org.eclipse.swt.widgets.Monitor; |
| 50 | +import org.eclipse.swt.widgets.Shell; |
| 51 | +import org.eclipse.swt.widgets.Text; |
| 52 | +import org.sofproject.topo.ui.graph.ITopoGraph; |
| 53 | +import org.sofproject.gst.json.JsonUtils; |
| 54 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 55 | + |
| 56 | +import org.sofproject.gst.topo.model.GstTopoGraph; |
| 57 | + |
| 58 | +public class JsonCustomOptionPane { |
| 59 | + |
| 60 | + ITopoGraph graph; |
| 61 | + |
| 62 | + private static boolean isInteger(String s) { |
| 63 | + try { |
| 64 | + Integer.parseInt(s); |
| 65 | + } catch (NumberFormatException e) { |
| 66 | + return false; |
| 67 | + } catch (NullPointerException e) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + public JsonCustomOptionPane(Display display, ITopoGraph graph) { |
| 74 | + this.graph = graph; |
| 75 | + Shell shell = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.MIN); |
| 76 | + |
| 77 | + shell.setText("Serialize JSON"); |
| 78 | + |
| 79 | + GridLayout gridLayout = new GridLayout(4, false); |
| 80 | + gridLayout.verticalSpacing = 8; |
| 81 | + shell.setLayout(gridLayout); |
| 82 | + |
| 83 | + new Label(shell, SWT.NULL).setText("Name:"); |
| 84 | + Text nameText = new Text(shell, SWT.SINGLE | SWT.BORDER); |
| 85 | + GridData nameGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); |
| 86 | + nameGridData.horizontalSpan = 3; |
| 87 | + nameText.setLayoutData(nameGridData); |
| 88 | + |
| 89 | + new Label(shell, SWT.NULL).setText("Version:"); |
| 90 | + Text versionText = new Text(shell, SWT.BORDER); |
| 91 | + GridData versionGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); |
| 92 | + versionGridData.horizontalSpan = 3; |
| 93 | + versionText.setLayoutData(versionGridData); |
| 94 | + |
| 95 | + new Label(shell, SWT.NULL).setText("Description:"); |
| 96 | + Text descriptionText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL); |
| 97 | + GridData descriptionGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); |
| 98 | + descriptionGridData.horizontalSpan = 3; |
| 99 | + descriptionGridData.heightHint = 100; |
| 100 | + descriptionGridData.widthHint = 200; |
| 101 | + descriptionText.setLayoutData(descriptionGridData); |
| 102 | + |
| 103 | + new Label(shell, SWT.NULL).setText("Type:"); |
| 104 | + Combo typeCombo = new Combo(shell, SWT.READ_ONLY); |
| 105 | + typeCombo.setBounds(50, 50, 200, 65); |
| 106 | + String items[] = { "Gstreamer", "Ffmpeg" }; |
| 107 | + typeCombo.setItems(items); |
| 108 | + typeCombo.setText(items[0]); |
| 109 | + GridData typeGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); |
| 110 | + typeGridData.horizontalSpan = 3; |
| 111 | + typeCombo.setLayoutData(typeGridData); |
| 112 | + |
| 113 | + Button okButton = new Button(shell, SWT.PUSH); |
| 114 | + okButton.setText("Ok"); |
| 115 | + GridData buttonGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); |
| 116 | + okButton.setLayoutData(buttonGridData); |
| 117 | + okButton.addSelectionListener(new SelectionAdapter() { |
| 118 | + |
| 119 | + @Override |
| 120 | + public void widgetSelected(SelectionEvent e) { |
| 121 | + if (nameText.getText().isEmpty()) { |
| 122 | + MessageBox messageBox = new MessageBox(shell, SWT.ERROR); |
| 123 | + messageBox.setMessage("Name cannot be empty!"); |
| 124 | + messageBox.open(); |
| 125 | + } else if (!isInteger(versionText.getText())) { |
| 126 | + MessageBox messageBox = new MessageBox(shell, SWT.ERROR); |
| 127 | + messageBox.setMessage("Version number should be an integer!"); |
| 128 | + messageBox.open(); |
| 129 | + } else { |
| 130 | + try { |
| 131 | + JsonProperty jsonProperty = new JsonProperty(nameText.getText(), descriptionText.getText(), |
| 132 | + versionText.getText(), typeCombo.getItem(typeCombo.getSelectionIndex())); |
| 133 | + new JsonUtils().serializeJson(jsonProperty, graph.getPipelinesString()); |
| 134 | + shell.close(); |
| 135 | + } catch (CoreException | IOException error) { |
| 136 | + error.printStackTrace(); // TODO: |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + Button cancelButton = new Button(shell, SWT.PUSH); |
| 143 | + cancelButton.setText("Cancel"); |
| 144 | + GridData cancelbuttonGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); |
| 145 | + cancelButton.setLayoutData(cancelbuttonGridData); |
| 146 | + cancelButton.addSelectionListener(new SelectionAdapter() { |
| 147 | + |
| 148 | + @Override |
| 149 | + public void widgetSelected(SelectionEvent e) { |
| 150 | + shell.close(); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + Monitor primary = display.getPrimaryMonitor(); |
| 155 | + Rectangle bounds = primary.getBounds(); |
| 156 | + Rectangle rect = shell.getBounds(); |
| 157 | + int x = bounds.x + (bounds.width - rect.width) / 2; |
| 158 | + int y = bounds.y + (bounds.height - rect.height) / 2; |
| 159 | + shell.setLocation(x, y); |
| 160 | + |
| 161 | + shell.pack(); |
| 162 | + shell.open(); |
| 163 | + while (!shell.isDisposed()) { |
| 164 | + if (!display.readAndDispatch()) |
| 165 | + display.sleep(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments