Пример #1
0
        // 插入一个新的字段
        public FieldLine NewField(int index)
        {
            EasyLine ref_line = null;

            if (index < this.Items.Count)
                ref_line = this.Items[index];

            if (ref_line is SubfieldLine)
            {
                ref_line = GetFieldLine(ref_line as SubfieldLine);
                if (ref_line == null)
                {
                    throw new Exception("index 为 " + index.ToString() + " 的子字段行没有找到字段行");
                }
                index = this.Items.IndexOf(ref_line);
                Debug.Assert(index != -1, "");
            }

            // 询问字段名
            NewSubfieldDialog dlg = new NewSubfieldDialog();
            GuiUtil.SetControlFont(dlg, this.Font);
            // dlg.Font = this.Font;

            dlg.Text = "新字段";
            dlg.AutoComplete = true;

            if (ref_line != null)
                dlg.NameString = ref_line.Name;
            else
                dlg.NameString = this.DefaultFieldName;
            dlg.MarcDefDom = this.MarcDefDom;
            dlg.Lang = this.Lang;

            dlg.StartPosition = FormStartPosition.CenterScreen;
            dlg.ShowDialog(this);

            if (dlg.DialogResult != DialogResult.OK)
                return null;

            bool bControlField = Record.IsControlFieldName(dlg.NameString);
            string strDefaultValue = "";
            string strIndicator = "  ";
            if (bControlField == false)
                strDefaultValue = new string((char)31, 1) + "a";

            List<string> results = null;
            string strError = "";
            // 获得宏值
            // parameters:
            //      strSubFieldName 子字段名。特殊地,如果为"#indicator",表示想获取该字段的指示符缺省值
            // return:
            //      -1  error
            //      0   not found 
            //      1   found
            int nRet = GetDefaultValue(
                0,  // index,
                dlg.NameString,
                "",
                out results,
                out strError);
            if (nRet == -1)
                MessageBox.Show(this, strError);

            if (results != null
                && results.Count > 0)
            {
                strDefaultValue = results[0];
                if (bControlField == false)
                    SplitDefaultValue(strDefaultValue,
                        out strIndicator,
                        out strDefaultValue);
            }

            bool bChanged = false;

            // 辅助剖析子字段的对象
            MarcField field_node = new MarcField(dlg.NameString, strIndicator, strDefaultValue);

            FieldLine field_line = new FieldLine(this);
            field_line.IsControlField = bControlField;
            field_line.Name = dlg.NameString;
            field_line.Caption = GetCaption(dlg.NameString, "", this._includeNumber);
            if (bControlField == false)
            {
                field_line.Indicator = strIndicator;  // 从 marcdef 中获得缺省的指示符值
                field_line.ExpandState = ExpandState.Expanded;
            }

            EasyLine after_line = null;
#if FIX
            bool bExpanded = false;
            bool bHideChanged = false;
#endif
            if (dlg.InsertBefore == true)
            {
                after_line = ref_line;
            }
            else
            {
                // 如果插入点是字段行,需要跳过下属的子字段行
                // EasyLine ref_line = this.Items[index];
                if (ref_line is FieldLine)
                {
                    List<EasyLine> lines = GetSubfieldLines(ref_line as FieldLine);
                    index += lines.Count + 1;
                }
                else
                    index++;

                if (index < this.Items.Count)
                    after_line = this.Items[index];
            }

#if FIX
            if (after_line != null)
            {
                // 如果插入位置后面一个字段是隐藏状态,则会出现故障,需要先修改为显示状态,插入后再隐藏
                if (after_line is FieldLine && after_line.Visible == false)
                {
                    this.ToggleHide(after_line as FieldLine);
                    bHideChanged = true;
                }
                // 如果本字段是收缩状态,则会出现故障,需要先修改为展开状态,插入后再收缩
                if (after_line.ExpandState == ExpandState.Collapsed)
                {
                    this.ToggleExpand(after_line);
                    Debug.Assert(after_line.ExpandState == ExpandState.Expanded, "");
                    bExpanded = true;
                }
            }
#endif

            InsertNewLine(index++,
    field_line,
    false);
            bChanged = true;

            // 如果必要,创建子字段对象
            foreach (MarcSubfield subfield_node in field_node.ChildNodes)
            {
                SubfieldLine subfield_line = new SubfieldLine(this);
                subfield_line.Name = subfield_node.Name;
                subfield_line.Caption = GetCaption(field_node.Name, subfield_node.Name, this._includeNumber);
                subfield_line.Content = subfield_node.Content;
                InsertNewLine(index++,
                    subfield_line,
                    false);
                bChanged = true;
            }

#if FIX
            // 把参考行恢复到以前的状态
            if (after_line != null && bExpanded == true)
            {
                this.ToggleExpand(after_line);
                Debug.Assert(after_line.ExpandState == ExpandState.Collapsed, "");
            }
            if (after_line != null && bHideChanged == true)
            {
                this.ToggleHide(after_line as FieldLine);
            }
#endif
            if (bChanged == true)
                this.FireTextChanged();

            return field_line;
        }
Пример #2
0
        // 插入一个新的子字段
        public SubfieldLine NewSubfield(int index)
        {
            EasyLine ref_line = this.Items[index];
            string strFieldName = "";
            if (ref_line is FieldLine)
            {
                // 只能后插 TODO: 让对话框不能选择前插
                strFieldName = (ref_line as FieldLine).Name;
            }
            else
            {
                FieldLine ref_field_line = GetFieldLine(ref_line as SubfieldLine);
                if (ref_field_line == null)
                    return null;    // 应该抛出异常?
                strFieldName = ref_field_line.Name;
            }

            // 询问子字段名
            NewSubfieldDialog dlg = new NewSubfieldDialog();
            dlg.Font = this.Font;
            dlg.Text = "新子字段";
            dlg.AutoComplete = true;

            dlg.ParentNameString = strFieldName;
            dlg.NameString = this.DefaultSubfieldName;
            dlg.MarcDefDom = this.MarcDefDom;
            dlg.Lang = this.Lang;

            dlg.StartPosition = FormStartPosition.CenterScreen;
            dlg.ShowDialog(this);

            if (dlg.DialogResult != DialogResult.OK)
                return null;

            if (ref_line is FieldLine)
            {
                // 只能后插
                dlg.InsertBefore = false;
            }

            string strDefaultValue = "";

            List<string> results = null;
            string strError = "";
            // 获得宏值
            // parameters:
            //      strSubFieldName 子字段名。特殊地,如果为"#indicator",表示想获取该字段的指示符缺省值
            // return:
            //      -1  error
            //      0   not found 
            //      1   found
            int nRet = GetDefaultValue(
                0,  // index,
                dlg.ParentNameString,
                dlg.NameString,
                out results,
                out strError);
            if (nRet == -1)
                MessageBox.Show(this, strError);

            if (results != null
                && results.Count > 0)
            {
                strDefaultValue = results[0];
            }

            bool bChanged = false;

            SubfieldLine subfield_line = new SubfieldLine(this);
            subfield_line.Name = dlg.NameString;
            subfield_line.Caption = GetCaption(dlg.ParentNameString, dlg.NameString, this._includeNumber);
            subfield_line.Content = strDefaultValue;
            if (dlg.InsertBefore == true)
            {
                InsertNewLine(index++,
                    subfield_line,
                    false);
                bChanged = true;
            }
            else
            {
                index++;
                InsertNewLine(index++,
                    subfield_line,
                    false);
                bChanged = true;
            }

            if (bChanged == true)
                this.FireTextChanged();

#if DEBUG
            this.Verify();
#endif

            return subfield_line;
        }