summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ContainerBindMountWindowLayout.java
blob: 3b542a102756a78d7757905d18bac9ea32d0fdc0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.openslx.dozmod.gui.window.layout;

import org.openslx.bwlp.thrift.iface.ImageSummaryRead;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.control.ComboBox;
import org.openslx.dozmod.gui.control.QLabel;
import org.openslx.dozmod.gui.helper.GridManager;
import org.openslx.dozmod.gui.helper.StatusHeader;
import org.openslx.dozmod.util.ContainerUtils;
import org.openslx.virtualization.configuration.container.ContainerBindMount;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ContainerBindMountWindowLayout extends JDialog {

	/**
	 * Version for serialization.
	 */
	private static final long serialVersionUID = -4410299500877608736L;

	private static final String title = "Add Bind Mount";

	protected final StatusHeader header;
	//	protected final QLabel lblBmSource;
	protected final QLabel lblBmTarget;
	protected final JTextField txtBmTarget;
	protected final QLabel lblBmOptions;
	protected final JTextField txtBmOptions;
	protected final JButton btnSave;
	protected final JButton btnCancel;

	protected static final String EMPTY_MARKER = "-";
	protected static final String[] TAGS = { "USER_HOME", "USER_TMP" };
	protected static String[] SOURCE_MOUNT_POINTS = { EMPTY_MARKER, TAGS[0], TAGS[1], "D", "E", "F", "G", "H",
			"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

	protected final ComboBox<ContainerMountChoice> cboSourceMount = new ComboBox<>(
			new ComboBox.ComboBoxRenderer<ContainerMountChoice>() {
				@Override public String renderItem(ContainerMountChoice choice) {
					if (choice == null)
						return "";
					return choice.name;
				}
			}, ContainerMountChoice.class);

	public ContainerBindMountWindowLayout(Window modalParent) {
		super(modalParent, title, ModalityType.APPLICATION_MODAL);

		setLayout(new BorderLayout());

		header = new StatusHeader(getContentPane(), "Source and Target Path Required");

		JPanel contentPanel = new JPanel();
		add(contentPanel, BorderLayout.CENTER);
		GridManager grid = new GridManager(contentPanel, 2, true, new Insets(2, 2, 2, 2));

		cboSourceMount.setModel(
				new DefaultComboBoxModel<>(generateMountChoices().toArray(new ContainerMountChoice[0])));
		cboSourceMount.setSelectedIndex(0);
		grid.add(new QLabel("Source"));
		grid.add(cboSourceMount).fill(true, false).expand(true, false);
		grid.nextRow();

		//		grid.add(lblBmSource);
		//		grid.add(cboSourceMountPoint).fill(true, false).expand(true, false);
		//		grid.nextRow();

		lblBmTarget = new QLabel("Target");
		txtBmTarget = new JTextField();
		grid.add(lblBmTarget);
		grid.add(txtBmTarget).fill(true, false).expand(true, false);
		grid.nextRow();

		lblBmOptions = new QLabel("Options");
		txtBmOptions = new JTextField();
		grid.add(lblBmOptions);
		grid.add(txtBmOptions).fill(true, false).expand(true, false);
		grid.nextRow();

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
		buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		buttonPane.add(Box.createHorizontalGlue());
		btnCancel = new JButton("Cancel");
		buttonPane.add(btnCancel);
		buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
		btnSave = new JButton("Save");
		buttonPane.add(btnSave);
		grid.add(buttonPane, 2).fill(true, false).expand(true, false);
		grid.finish(false);

		setSize(350, 300);
		setResizable(false);
		if (modalParent != null) {
			Gui.centerShellOverShell(modalParent, this);
		}
	}

	private ArrayList<ContainerMountChoice> generateMountChoices() {
		List<ImageSummaryRead> dataContainerImages = ContainerUtils.getDataContainerImages();
		ArrayList<ContainerMountChoice> mountChoices = new ArrayList<>();
		mountChoices.add(ContainerMountChoice.getEmptyChoice());

		for (int i = 1; i < SOURCE_MOUNT_POINTS.length; i++) {
			mountChoices.add(new ContainerMountChoice(ContainerBindMount.ContainerMountType.DEFAULT,
					SOURCE_MOUNT_POINTS[i], SOURCE_MOUNT_POINTS[i]));
		}

		for (ImageSummaryRead image : dataContainerImages) {
			mountChoices.add(new ContainerMountChoice(ContainerBindMount.ContainerMountType.CONTAINER_IMAGE,
					image.imageName, image.imageBaseId));
		}
		return mountChoices;
	}

	protected static class ContainerMountChoice {

		public final ContainerBindMount.ContainerMountType type;
		public final String name;
		public final String value;

		private static ContainerMountChoice emptyMountChoice = null;

		public ContainerMountChoice(ContainerBindMount.ContainerMountType type, String name, String value) {
			this.type = type;
			this.name = name;
			this.value = value;
		}

		public static ContainerMountChoice getEmptyChoice() {
			if (emptyMountChoice == null)
				emptyMountChoice = new ContainerMountChoice(ContainerBindMount.ContainerMountType.DEFAULT,
						"-", "-");
			return emptyMountChoice;
		}
	}
}